| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <!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>
- /**
- * 原型链继承
- * 实现:
- * 子类的原型 = 父类的实例 实例顺着父类的原型链找到父类的属性和方法
- * 优点:写法简单 子类可以直接继承父类原型的方法
- * 缺点:
- * 父类中引用数据类型 共享的实例 若发生修改 则全部修改
- * 父类不能进行传参
- */
- function Person() {
- this.name = 'tutu';
- this.age = 3;
- this.list = ["吃饭", "睡觉", "打豆豆"];
- }
- Person.prototype.say = function () {
- console.log("hello");
- }
- function Child() { };
- Child.prototype = new Person();
- let c1 = new Child();
- let c2 = new Child();
- c1.list.push("玩耍");
- console.log(c1, 'c1')
- console.log(c2, 'c2')
- c1.say();
- </script>
- </body>
- </html>
|