From a895a64b404ed4daaa01f4b127f4ac0ee1c57a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E7=8E=AE=E5=96=86?= Date: Mon, 7 Nov 2022 12:56:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/2022.11.04.html" | 71 +++++++++++++++++++ .../2022.11.04-\347\273\247\346\211\277.md" | 53 ++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 "04\351\203\221\347\216\256\345\226\206/\344\275\234\344\270\232/2022.11.04.html" create mode 100644 "04\351\203\221\347\216\256\345\226\206/\347\254\224\350\256\260/2022.11.04-\347\273\247\346\211\277.md" diff --git "a/04\351\203\221\347\216\256\345\226\206/\344\275\234\344\270\232/2022.11.04.html" "b/04\351\203\221\347\216\256\345\226\206/\344\275\234\344\270\232/2022.11.04.html" new file mode 100644 index 0000000..c74e1ec --- /dev/null +++ "b/04\351\203\221\347\216\256\345\226\206/\344\275\234\344\270\232/2022.11.04.html" @@ -0,0 +1,71 @@ + + + + + + + + Document + + + + + + + \ No newline at end of file diff --git "a/04\351\203\221\347\216\256\345\226\206/\347\254\224\350\256\260/2022.11.04-\347\273\247\346\211\277.md" "b/04\351\203\221\347\216\256\345\226\206/\347\254\224\350\256\260/2022.11.04-\347\273\247\346\211\277.md" new file mode 100644 index 0000000..a2b411f --- /dev/null +++ "b/04\351\203\221\347\216\256\345\226\206/\347\254\224\350\256\260/2022.11.04-\347\273\247\346\211\277.md" @@ -0,0 +1,53 @@ +# 11、继承 + +## 原型链继承 + +通过实例化一个新的函数,子类的原型指向了父类的实例,子类就可以调用其父类原型对象上的私有属性和公有方法。(本质就是重写了原型对象) + + function Parent() { +     this.parentName = '父类'; + } + Parent.prototype.getParentName = function() { +     return this.parentName; + }; + function Child() { +     this.childName = '子类'; + } + Child.prototype = new Parent(); + Child.prototype.getChildName = function() { +     return this.childName + }; + var c = new Child(); + console.log(c.getParentName()); // '父类' + +借用构造函数继承 + + function Parent(name) { +     this.name = name; +     this.hobbies = ["sing", "dance", "rap"]; + } + function Child(name) { +     Parent.call(this, name); this.age = 24 + } + var c1 = new Child('c1'); + var c2 = new Child('c2'); + c1.hobbies.push('coding'); + console.log(c1.hobbies) + console.log(c2.hobbies) + console.log(c1 instanceof Parent) + console.log(c1 instanceof Child) + +共享原型继承 + +这种方式下子类和父类共享一个原型 + + function Parent(){} +     Parent.prototype.hobbies = ["sing", "dance", "rap"]; + function Child(name, age){ +     this.name = name; this.age = age; + } + Child.prototype = Parent.prototype; + var c1 = new Child("c1", 20); + var c2 = new Child("c2", 24); + c1.hobbies.push("coding"); + console.log(c1.hobbies); // ["sing", "dance", "rap", "coding"] console.log(c2.hobbies); // ["sing", "dance", "rap", "coding"] console.log(c1.name); // "c1" console.log(c2.name); // "c2" -- Gitee From 4a683947c746fd7204a861c5782afcd3ba5eedcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E7=8E=AE=E5=96=86?= Date: Mon, 14 Nov 2022 13:24:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=B8=89=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022.11.07-\347\273\247\346\211\277.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "04\351\203\221\347\216\256\345\226\206/\347\254\224\350\256\260/2022.11.07-\347\273\247\346\211\277.md" diff --git "a/04\351\203\221\347\216\256\345\226\206/\347\254\224\350\256\260/2022.11.07-\347\273\247\346\211\277.md" "b/04\351\203\221\347\216\256\345\226\206/\347\254\224\350\256\260/2022.11.07-\347\273\247\346\211\277.md" new file mode 100644 index 0000000..b8ff7b6 --- /dev/null +++ "b/04\351\203\221\347\216\256\345\226\206/\347\254\224\350\256\260/2022.11.07-\347\273\247\346\211\277.md" @@ -0,0 +1,37 @@ +## 最终版圣杯模式 + + var inherit = (function(){ //立即执行函数 + var F = function(){}; + return function(Target,Origin){ + F.prototype = Origin.prototype; + Target.prototype = new F(); + Target.prototype.constructor = Target; + Target.prototype.uber = Origin.prototype; + } + }()) + inherit(Son,Father) + var son = new Son(); + var father = new Father(); + +## class继承 + + class Car { +     constructor(brand) { +         this.carname = brand; +     } +     present() { +     return 'I have a ' + this.carname; +     } + } + class Model extends Car { +     constructor(brand, mod) { +         super(brand); +         this.model = mod; +     } +     show() { +     return this.present() + ', it is a ' + this.model; +     } + } + + let myCar = new Model("Ford", "Mustang"); + document.getElementById("demo").innerHTML = myCar.show(); -- Gitee