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

1234567891011121314151617181920212223242526
  1. // 普通函数
  2. // function Person() {
  3. // console.log('aa');
  4. // }
  5. // // 构造函数
  6. // function Person() {
  7. // constructor() {
  8. // }
  9. // }
  10. // Person.prototype => 对象的原型
  11. // new Person();
  12. //类
  13. class Person1 {
  14. constructor(name, age) {
  15. this.name = name;
  16. this.age = age;
  17. // 当前对象
  18. console.log(this, 'this');
  19. }
  20. // 类方法
  21. hello() {
  22. console.log("你好", this);
  23. }
  24. }
  25. let p1 = new Person1("哪吒", 3);
  26. p1.hello();