1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <!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>
- /**
- * 1.原型链继承:
- * 构造函数 原型 实例
- */
- function Father() {
- this.name = "Lucy";
- this.arr = [1, 2, 3];
- }
- function Child() {
- this.age = 18;
- }
- Child.prototype = new Father();
- let newChild = new Child();
- console.log(newChild.arr,'新的')
- let a1 = new Child();
- let a2 = new Child();
- /**
- * 因为两个实例使用的是同一个原型,所以一个修改另一个也会发生改变
- */
- a2.arr.push(10);
- a1.name += 10; //a1.name = a1.name + 10
- a2.name += 20;
- console.log(a1,'a1');
- console.log(a2,'a2');
- </script>
- </body>
- </html>
|