理解js對象繼承的N種模式,js對象繼承

來源:互聯網
上載者:User

理解js對象繼承的N種模式,js對象繼承

本文分享了js對象繼承的N種模式,供大家參考。

一、原型鏈繼承

function Person(){};Person.prototype = {  constructor: Person,  name: "Oliver"};    function People(){};People.prototype = new Person();People.prototype.constructor = People;People.prototype.sayName = function(){  return this.name;};var ins = new People();console.log(ins.sayName());

二、借用建構函式(偽造對象,經典繼承)

1、無參數

function SuperType(){  this.color = ["red","yellow","white"];}function SubType(){  SuperType.call(this);}var instance1 = new SubType();var instance2 = new SubType();instance1.color.pop();console.log(instance1.color); //["red", "yellow"]console.log(instance2.color); //["red", "yellow", "white"]

2、有參數

function SuperType(name){  this.name = name;  this.number = [21,32,14,1];}function SubType(name,age){  SuperType.call(this,name);  this.age = age;}var instance1 = new SubType("Oliver",18);var instance2 = new SubType("Troy",24);instance2.number.pop();console.log(instance1.name + instance1.age + instance1.number); //Oliver1821,32,14,1console.log(instance2.name + instance2.age + instance2.number); //Troy2421,32,14

三、組合繼承(偽經典繼承)

1、無參數

function SuperType(){  this.color = ["red","yellow","white"];}SuperType.prototype.sayColor = function(){  return this.color;};function SubType(){  SuperType.call(this);  this.number = 321;}SubType.prototype = new SuperType();SubType.prototype.constructor = SubType;SubType.prototype.sayNumber = function(){  return this.number;};var instance1 = new SubType();var instance2 = new SubType();instance2.color.pop();console.log(instance1.color + instance1.number); //red,yellow,white321console.log(instance2.color + instance2.number); //red,yellow321

2、有參數

function SuperType(name){  this.name = name;  this.number = [32,1342,11,1];}SuperType.prototype.sayName = function(){  return this.name;};function SubType(name,age){  SuperType.call(this,name);  this.age = age;}SubType.prototype = new SuperType();SubType.prototype.constructor = SubType;SubType.prototype.sayAge = function(){  return this.age;};var instance1 = new SubType("Oliver",18);var instance2 = new SubType("Troy",24);instance2.number.pop();console.log(instance1.sayName() + instance1.sayAge() + instance1.number); //Oliver1832,1342,11,1console.log(instance2.sayName() + instance2.sayAge() + instance2.number); //Troy2432,1342,11

三、寄生組合式繼承(參考型別最理想的範式)

function inheritPrototype(subType,superType){  var prototype = Object(superType.prototype);  prototype.constructor = subType;  subType.prototype = prototype;}function SuperType(name){  this.name = name;  this.number = [321,321,43];}SuperType.prototype.sayName = function(){  return this.name;};function SubType(name,age){  SuperType.call(this,name);  this.age = age;}inheritPrototype(SubType,SuperType);SubType.prototype.sayAge = function(){  return this.age;};var instance1 = new SubType("Oliver",18);var instance2 = new SubType("Troy",24);instance2.number.pop();console.log(instance1.sayName() + instance1.sayAge() + instance1.number); //Oliver18321,321,43console.log(instance2.sayName() + instance2.sayAge() + instance2.number); //Troy24321,321

或者可以把inheritPrototype 函數寫成下面這樣:

function inheritPrototype(SubType,SuperType){  SubType.prototype = new SuperType();  SubType.prototype.constructor = SubType;}

四、原型式繼承(用於共用參考型別的值,與寄生式類似)

1、傳統版(先定義object() 函數,再繼承)

function object(o){  function F(){};  F.prototype = o;  return new F();}var SuperType = {  name: "Oliver",  number: [321,321,4532,1]};var SubType1 = object(SuperType);var SubType2 = object(SuperType);SubType1.name = "Troy";SubType1.number.pop();SubType2.name = "Alice";SubType2.number.pop();console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

ECMAScript 5 版(直接用Object.create(),再繼承)

var SuperType = {  name: "Oliver",  number: [321,321,4532,1]};var SubType1 = Object.create(SuperType); //省略了定義object()函數var SubType2 = Object.create(SuperType);SubType1.name = "Troy";SubType1.number.pop();SubType2.name = "Alice";SubType2.number.pop();console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

ECMAScript 5 簡寫版(定義Object.create()的第二個參數,再繼承)

var SuperType = {  name: "Oliver",  number: [321,321,4532,1]};var SubType1 = Object.create(SuperType,{  name: {    value : "Troy"  }});var SubType2 = Object.create(SuperType,{  name: {    value : "Alice"  }});SubType1.number.pop();SubType2.number.pop();console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

寄生式繼承(用於共用參考型別的值,與原型式類似)

function createAnother(original){  var clone = Object(original);  clone.sayHi = function(){    return "Hi";  };  return clone;}var person = {  name: "Oliver",  number: [13,21,31,1]};var anotherPerson = createAnother(person);anotherPerson.number.pop();console.log(anotherPerson.sayHi() + anotherPerson.number); //Hi13,21,31console.log(person.number); //13,21,31

以上就是本文的全部內容,希望對大家的學習有所協助。

您可能感興趣的文章:
  • javascript 物件導向繼承
  • 由JavaScript中call()方法引發的對物件導向繼承機制call的思考
  • javaScript物件導向繼承方法經典實現
  • 使用apply方法實現javascript中的對象繼承
  • Javascript中對象繼承的實現小例
  • js對象繼承之原型鏈繼承執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.