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

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