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

12345678910111213141516
  1. // 构造函数
  2. // function fn1() {
  3. // };
  4. // fn1();
  5. // new fn1();
  6. class Person1 {
  7. constructor(name, age) {
  8. this.name = name;
  9. this.age = age;
  10. }
  11. hello() {
  12. console.log("hi");
  13. }
  14. }
  15. let p2 = new Person1('八戒', 10);
  16. p2.hello();