2.构造函数和this.js 386 B

123456789101112131415161718
  1. // 构造函数 => 函数
  2. // function Fn1() {
  3. // }
  4. // Fn1();
  5. // new Fn1();
  6. // 构造函数:构造器(构造函数)(属性) 原型(方法)
  7. class Person1 {
  8. constructor(name, age) {
  9. this.name = name;
  10. this.age = age;
  11. console.log(this, 'this');
  12. }
  13. hello() {
  14. console.log("你好");
  15. }
  16. }
  17. let p1 = new Person1('孙悟空', 20);
  18. p1.hello();