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

12345678910111213141516171819202122
  1. "use strict";
  2. // 构造函数 => 函数
  3. // function fn1() {
  4. // console.log(this)
  5. // }
  6. // fn1();
  7. // new fn1();
  8. // 构造函数:原型 构造器
  9. class Person1 {
  10. constructor(name, age) {
  11. this.name = name;
  12. this.age = age;
  13. console.log(this);
  14. }
  15. hello() {
  16. console.log(this, '哈哈哈');
  17. }
  18. }
  19. let p1 = new Person1("孙悟空", 20);
  20. let p2 = new Person1("猪八戒", 22);
  21. p1.hello();
  22. // new Person1("孙悟空",20).hello();