javascript“類”與繼承總結和回顧2

來源:互聯網
上載者:User

前面一篇文章,我簡單的講了Es6以前js實現“類”的三種方式。但是繼承的方式,我只簡單寫了一下極簡主義繼承。這篇文章在上篇文章基礎之上,我再寫寫js繼承的幾種方式,最後再和大家一起看看ES6中類的實現方式。

js繼承

關於繼承,網上也有很多資料可以查詢,但是很多朋友對其理解的不夠深入。講到繼承,假如你不去查資料,猛地一下,可能說不出個所以然來。這就說明我們的基礎知識打的不夠紮實。沒有理解透徹。今天,我站在前輩的基礎之上,再來和大家一起回顧一下這個 繼承。

繼承最常用的兩種方式如下:

原型鏈繼承(對象間的繼承)
類式繼承(建構函式間的繼承)
原型鏈繼承

什麼是原型鏈繼承?這裡我就不說什麼定義了。其實就是用prototype繼承父級。

舉個例子:

function Parent(){
    this.name = 'mike';
}
function Child(){
    this.age = 12;
}
Child.prototype = new Parent();//Child繼承Parent,通過原型,形成鏈條

var test = new Child();
alert(test.age);
alert(test.name);//得到被繼承的屬性
//繼續原型鏈繼承
function Brother(){   //brother構造
    this.weight = 60;
}
Brother.prototype = new Child();//繼續原型鏈繼承
var brother = new Brother();
alert(brother.name);//繼承了Parent和Child,彈出mike
alert(brother.age);//彈出12
上面例子,通過原型,形成鏈條,Child繼承了Parent,Brother有繼承了Child,最後brother既有Child和Parent的屬性,又有自己的屬性。這就是原形鏈的繼承。

類式繼承(借用建構函式)

類式繼承一般是通過運用call或者apply 進行子類型建構函式的內部調用超類型的建構函式。關於call和apply的用法,請看我之前文章:http://www.haorooms.com/post/js_constructor_pro 純粹的運用這種方式,項目中通常比較少見,一般都是組合著運用。

舉個例子:

function Parent(age){
    this.name = ['mike','jack','smith'];
    this.age = age;
}

function Child(age){
    Parent.call(this,age);
}
var test = new Child(21);
alert(test.age);//21
alert(test.name);//mike,jack,smith
test.name.push('bill');
alert(test.name);//mike,jack,smith,bill
上面的兩種繼承是最基本的兩種繼承方式。

此外還有一些繼承方式,我們一起來看一下!

組合繼承

組合繼承通常是上面兩種繼承方式組合在一起使用的繼承方式。

舉例如下:

function Parent(age){
    this.name = ['mike','jack','smith'];
    this.age = age;
}
Parent.prototype.run = function () {
    return this.name  + ' are both' + this.age;
};
function Child(age){
    Parent.call(this,age);//對象冒充,給超類型傳參
}
Child.prototype = new Parent();//原型鏈繼承
var test = new Child(21);//寫new Parent(21)也行
alert(test.run());//mike,jack,smith are both21
原型式繼承

和上面講的原形鏈式繼承只有一字之差,但是不是同一個內容。我們所說的原型式繼承,就是我們上節課所講的,通過Object.create()方式來建立新的類。因為這種方式老式瀏覽器不支援。因此,假如我們不用Object.create(),也可以有替代法,方式如下:

 function obj(o){
     function F(){}
     F.prototype = o;
     return new F();
 }
這個函數,就實現了我們Object.create()建立類的方式!

因此舉例如下:

 function obj(o){
     function F(){}
     F.prototype = o;
     return new F();
 }
var box = {
    name : 'trigkit4',
    arr : ['brother','sister','baba']
};
var b1 = obj(box);
alert(b1.name);//trigkit4

b1.name = 'mike';
alert(b1.name);//mike

alert(b1.arr);//brother,sister,baba
b1.arr.push('parents');
alert(b1.arr);//brother,sister,baba,parents

var b2 = obj(box);
alert(b2.name);//trigkit4
alert(b2.arr);//brother,sister,baba,parents
寄生式繼承

舉例如下:

function creatAnother(original){
    var clone=new Object(original);
    clone.sayHi=function(){
        alert("hi")
    };
    return clone;
}
var person={
    name:"haorooms",
    friends:["hao123","zhansan","lisi"]
}

var antherPerson=creatAnother(person);
antherPerson.sayHi();//hi
寄生組合式

function inheritPrototype (subType,superType) {
    var prototype = Object.creat(superType.prototype);
    prototype.constructor = subType;
    subType.prototype = prototype;
};

function SuperType (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function () {
    console.log(this.name);
}
function SubType(name, age) {
    //繼承屬性
    SuperType.call(this,name);
    this.age = age;
}
//繼承方法
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge = function () {
    console.log(this.age);
}

var instance = new SubType();
類的繼承,基本上就是如上幾種方式。下面再簡單的說一下ES6的class類吧!

es6實作類別

//定義類
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  toString() {
    return '('+this.x+', '+this.y+')';
  }
}
var point = new Point(2, 3);
point.toString() // (2, 3)
point.hasOwnProperty('x') // true
point.hasOwnProperty('y') // true
point.hasOwnProperty('toString') // false
point.__proto__.hasOwnProperty('toString') // true
Class的繼承

class ColorPoint extends Point {
  constructor(x, y, color) {
    super(x, y); // 調用父類的constructor(x, y)
    this.color = color;
  }  
  toString() {
    return this.color + ' ' + super.toString(); // 調用父類的toString()
  }
}
由於Es6大多數瀏覽器現在還不支援,緊緊在reactNative或者node中使用的較多,親們可以先看看其定義類和繼承類的方式!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.