2.构造函数和this.ts 559 B

123456789101112131415161718192021222324
  1. /** 函数和构造函数的区别
  2. * 1.名称首字母大写:
  3. * 函数:小驼峰命名法
  4. * 构造函数:大驼峰命名法
  5. * 2.调用方式不同
  6. * 函数:fn()
  7. * 构造函数:new Fn()
  8. * 3.this指向不同
  9. * 函数:指向window
  10. * 构造函数:指向实例对象
  11. */
  12. // 构造函数:prototype constructor
  13. class Person1 {
  14. name:string;
  15. age:number;
  16. constructor(x:string,y:number) {
  17. this.name = x;
  18. this.age = y;
  19. console.log(this);
  20. }
  21. }
  22. let p2 = new Person1('孙悟空',20);