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

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