JavaScript 創(chuàng)建對(duì)象的多種方式以及優(yōu)缺點(diǎn)

JavaScript 創(chuàng)建對(duì)象的多種方式以及優(yōu)缺點(diǎn)

1.工廠模式

function createPerson(name) {
  var o = new Object();
  o.name = name;
  o.getName = function() {
    console.log(this.name)
  };
  return o;
}

var person1 = createPerson('cause');

缺點(diǎn):對(duì)象無法識(shí)別羽氮,因?yàn)樗械膶?shí)例都指向一個(gè)原型

2.構(gòu)造函數(shù)模式

function Person(name) {
  this.name = name;
  this.getName = function() {
    console.log(this.name);
  }
}

var person = new Person('cause');

優(yōu)點(diǎn):實(shí)例可以識(shí)別為一個(gè)特定的類型

缺點(diǎn):每次創(chuàng)建實(shí)例時(shí)惫恼,每個(gè)方法都要被創(chuàng)建一次

2.1 構(gòu)造函數(shù)模式優(yōu)化

function Person(name) {
  this.name = name;
  this.getName = getName;
  }
}
function getName() {
  console.log(this.name);
}

var person = new Person('cause');

優(yōu)點(diǎn):解決了每個(gè)方法都要被重新創(chuàng)建的問題

缺點(diǎn):這叫啥封裝……

3.1 原型模式

function Person(name) {
 
}
 
Person.prototype = {
    name: 'cause',
    getName: function () {
        console.log(this.name);
    }
};
 
var person1 = new Person();

優(yōu)點(diǎn):封裝性好了一點(diǎn)

缺點(diǎn):重寫了原型,丟失了constructor屬性

3.2 原型模式優(yōu)化

function Person(name) {

}
Person.prototype = {
  constructor: Person,
  name: "cause",
  getName: function() {
    console.log(this.name);
  }
};

var person1 = new Person();

優(yōu)點(diǎn):實(shí)例可以通過constructor屬性找到所屬構(gòu)造函數(shù)

缺點(diǎn):原型模式該有的缺點(diǎn)還是有

4. 組合模式

構(gòu)造函數(shù)模式與原型模式雙劍合璧令宿。

function Person(name) {
  this.name = name;
}
Person.prototype = {
  constructor: Person,
  getName: function() {
    console.log(this.name);
  }
};

var person1 = new Person();

優(yōu)點(diǎn):該共享的共享粒没,該私有的私有,使用最廣泛的方式

缺點(diǎn):有的人就是希望全部都寫在一起癞松,即更好的封裝性

繼承的多種方式和優(yōu)缺點(diǎn)

1.原型鏈繼承

function Parent() {
  this.name = "cause";
}
Parent.prototype.getName = function() {
  console.log(this.name);
}
function Child() {

}
Child.prototype = new Parent();

var child1 = new Child();
console.log(child1.getName()); // cause

問題:

1.引用類型的屬性被所有實(shí)例共享,舉個(gè)例子:

function Parent() {
  this.names = ['cause', 'xl'];
}
function Child() {

}

Child.prototype = new Parent();

var child1 = new Child();
console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy", "yayu"]

2.在創(chuàng)建 Child 的實(shí)例時(shí)硕勿,不能向Parent傳參

2.借用構(gòu)造函數(shù)(經(jīng)典繼承)

function Parent() {
  this.names = ['cause', 'xl'];
}
function Child() {
  Parent.call(this);
}

var child1 = new Child();
child1.names.push('cayden');
console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy"]

優(yōu)點(diǎn):

1.避免了引用類型的屬性被所有實(shí)例共享

2.可以在 Child 中向 Parent 傳參

舉個(gè)例子:

function Parent (name) {
    this.name = name;
}

function Child (name) {
    Parent.call(this, name);
}

var child1 = new Child('kevin');
console.log(child1.name); // kevin

var child2 = new Child('daisy');
console.log(child2.name); // daisy

缺點(diǎn):

方法都在構(gòu)造函數(shù)中定義源武,每次創(chuàng)建實(shí)例都會(huì)創(chuàng)建一遍方法言秸。

3.組合繼承

原型鏈繼承和經(jīng)典繼承雙劍合璧。

function Parent (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
    console.log(this.name)
}

function Child (name, age) {
    Parent.call(this, name);
    this.age = age;
}

Child.prototype = new Parent();

var child1 = new Child('kevin', '18');

child1.colors.push('black');

console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]

var child2 = new Child('daisy', '20');

console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]

優(yōu)點(diǎn):融合原型鏈繼承和構(gòu)造函數(shù)的優(yōu)點(diǎn)查排,是 JavaScript 中最常用的繼承模式。

精益求精

function Parent (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
    console.log(this.name)
}

function Child (name, age) {
    Parent.call(this, name);
    this.age = age;
}

// 關(guān)鍵的三步
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();

var child1 = new Child('kevin', '18');

最后我們封裝一下這個(gè)繼承方法:

function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

function prototype(child, parent) {
    var prototype = object(parent.prototype);
    prototype.constructor = child;
    child.prototype = prototype;
}

// 當(dāng)我們使用的時(shí)候:
prototype(Child, Parent);

更多閱讀

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市叛买,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌刻伊,老刑警劉巖椒功,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異丁屎,居然都是意外死亡旱眯,警方通過查閱死者的電腦和手機(jī)证九,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門共虑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人看蚜,你說我怎么就攤上這事】事撸” “怎么了音诫?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)梨撞。 經(jīng)常有香客問我香罐,道長(zhǎng)卧波,這世上最難降的妖魔是什么庇茫? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任旦签,我火速辦了婚禮查坪,結(jié)果婚禮上宁炫,老公的妹妹穿的比我還像新娘。我一直安慰自己望忆,他們只是感情好竿秆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般常摧。 火紅的嫁衣襯著肌膚如雪威创。 梳的紋絲不亂的頭發(fā)上谎懦,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音吸申,去河邊找鬼享甸。 笑死,一個(gè)胖子當(dāng)著我的面吹牛日丹,可吹牛的內(nèi)容都是我干的蚯嫌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼择示,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了汪诉?” 一聲冷哼從身側(cè)響起剪菱,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎孝常,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體上渴,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡喜颁,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年半开,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了隔披。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片寂拆。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖鬓长,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情涉波,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布苍日,位于F島的核電站城侧,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏嫌佑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一揩魂、第九天 我趴在偏房一處隱蔽的房頂上張望炮温。 院中可真熱鬧,春花似錦柒啤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至拳话,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間弃衍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工姜钳, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人哥桥。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓拟糕,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親送滞。 傳聞我的和親對(duì)象是個(gè)殘疾皇子辱挥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容