| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- /**
- * 构造函数继承
- * 实现:
- * 在子类的构造函数中 用call/apply把父类的this指向子类的实例 相当于把父类的属性全部复制一份
- * 优点:
- * 可以给父类进行传参
- * 父类中引用数据类型 不共享实例
- * 缺点:
- * 不能继承父类原型上的方法
- */
- function Person(name1) {
- this.name = name1;
- this.age = 3;
- this.list = ["吃饭", "睡觉", "打豆豆"];
- }
- Person.prototype.say = function () {
- console.log("hello");
- }
- function Child(name1) {
- Person.apply(this,[name1]);
- // Person.call(this,name1);
- };
- let c1 = new Child('图图');
- let c2 = new Child('喜羊羊');
- c1.list.push("玩耍");
- console.log(c1, 'c1')
- console.log(c2, 'c2')
- // c1.say(); // TypeError: c1.say is not a function
- </script>
- </body>
- </html>
|