zoukankan      html  css  js  c++  java
  • JS继承的6种方法

    1.原型链

    基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。

    构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。

    原型链实现继承例子:

     1 function SuperType() {
     2 this.property = true;
     3 }
     4 SuperType.prototype.getSuperValue = function() {
     5 return this.property;
     6 }
     7 function subType() {
     8 this.property = false;
     9 }
    10 //继承了SuperType
    11 SubType.prototype = new SuperType();
    12 SubType.prototype.getSubValue = function (){
    13 return this.property;
    14 }
    15 var instance = new SubType();
    16 console.log(instance.getSuperValue());//true

    2.借用构造函数

    基本思想:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。

    例子:

     1 11
     2 function SuperType() {
     3 this.colors = ["red","blue","green"];
     4 }
     5 function SubType() {
     6 SuperType.call(this);//继承了SuperType
     7 }
     8 var instance1 = new SubType();
     9 instance1.colors.push("black");
    10 console.log(instance1.colors);//"red","blue","green","black"
    11 var instance2 = new SubType();
    12 console.log(instance2.colors);//"red","blue","green"

    3.组合继承

    基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。

    例子:

     1 function SuperType(name) {
     2 this.name = name;
     3 this.colors = ["red","blue","green"];
     4 }
     5 SuperType.prototype.sayName = function() {
     6 console.log(this.name);
     7 }
     8 function SubType(name, age) {
     9 SuperType.call(this,name);//继承属性
    10 this.age = age;
    11 }
    12 //继承方法
    13 SubType.prototype = new SuperType();
    14 Subtype.prototype.constructor = Subtype;
    15 Subtype.prototype.sayAge = function() {
    16 console.log(this.age);
    17 }
    18 var instance1 = new SubType("EvanChen",18);
    19 instance1.colors.push("black");
    20 consol.log(instance1.colors);//"red","blue","green","black"
    21 instance1.sayName();//"EvanChen"
    22 instance1.sayAge();//18
    23 var instance2 = new SubType("EvanChen666",20);
    24 console.log(instance2.colors);//"red","blue","green"
    25 instance2.sayName();//"EvanChen666"
    26 instance2.sayAge();//20

    4.原型式继承

    基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。

    原型式继承的思想可用以下函数来说明:

    1 function object(o) {
    2 function F(){}
    3 F.prototype = o;
    4 return new F();
    5 }

    例子:

     1 var person = {
     2 name:"EvanChen",
     3 friends:["Shelby","Court","Van"];
     4 };
     5 var anotherPerson = object(person);
     6 anotherPerson.name = "Greg";
     7 anotherPerson.friends.push("Rob");
     8 var yetAnotherPerson = object(person);
     9 yetAnotherPerson.name = "Linda";
    10 yetAnotherPerson.friends.push("Barbie");
    11 console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

    ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和一个作为新对象定义额外属性的对象。

     1 var person = {
     2 name:"EvanChen",
     3 friends:["Shelby","Court","Van"];
     4 };
     5 var anotherPerson = Object.create(person);
     6 anotherPerson.name = "Greg";
     7 anotherPerson.friends.push("Rob");
     8 var yetAnotherPerson = Object.create(person);
     9 yetAnotherPerson.name = "Linda";
    10 yetAnotherPerson.friends.push("Barbie");
    11 console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

    5.寄生式继承

    基本思想:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象。

    例子:

     1 function createAnother(original) {
     2 var clone = object(original);
     3 clone.sayHi = function () {
     4 alert("hi");
     5 };
     6 return clone;
     7 }
     8 var person = {
     9 name:"EvanChen",
    10 friends:["Shelby","Court","Van"];
    11 };
    12 var anotherPerson = createAnother(person);
    13 anotherPerson.sayHi();///"hi"

    6.寄生组合式继承

    基本思想:通过借用函数来继承属性,通过原型链的混成形式来继承方法

    其基本模型如下所示:

    function inheritProperty(subType, superType) {
    var prototype = object(superType.prototype);//创建对象
    prototype.constructor = subType;//增强对象
    subType.prototype = prototype;//指定对象
    }

    例子:

     1 function SuperType(name){
     2 this.name = name;
     3 this.colors = ["red","blue","green"];
     4 }
     5 SuperType.prototype.sayName = function (){
     6 alert(this.name);
     7 };
     8 function SubType(name,age){
     9 SuperType.call(this,name);
    10 this.age = age;
    11 }
    12 inheritProperty(SubType,SuperType);
    13 SubType.prototype.sayAge = function() {
    14 alert(this.age);
    15 }
  • 相关阅读:
    BZOJ 1096: [ZJOI2007]仓库建设 动态规划 + 斜率优化
    CF576D Flights for Regular Customers 矩阵乘法 + Bitset优化
    BZOJ 3879: SvT 虚树 + 后缀自动机
    CF613D Kingdom and its Cities 虚树 + 树形DP
    luogu P4103 [HEOI2014]大工程 虚树 + 树形 DP
    BZOJ 2286: [Sdoi2011]消耗战 虚树
    组合数学入门
    对于下发的文件进行爬取,减少人去下载的过程
    写了个爬虫代理ip的脚本给大家使用
    文本属性,边界圆角,背景属性,精灵图案例
  • 原文地址:https://www.cnblogs.com/yangguoe/p/8461582.html
Copyright © 2011-2022 走看看