成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

js面向?qū)ο?

LeviDing / 2944人閱讀

摘要:創(chuàng)建對象的方法字面量優(yōu)點(diǎn)簡單方便缺點(diǎn)每創(chuàng)建一個對象都要重新寫,且創(chuàng)建多個比較占內(nèi)存工廠模式優(yōu)點(diǎn)改善了字面量方法創(chuàng)建多個相似對象的問題,不需要編寫重復(fù)的代碼缺點(diǎn)沒有解決對象是別的問題,不知道對象的類型是什么構(gòu)造函數(shù)模式優(yōu)點(diǎn)創(chuàng)建并定義了對象類型

創(chuàng)建對象的方法

1.字面量

// 優(yōu)點(diǎn):簡單方便
// 缺點(diǎn):每創(chuàng)建一個對象都要重新寫,且創(chuàng)建多個比較占內(nèi)存
var obj1 = {
    name: "liuhui1"
};
var obj2 = new Object({
    name: "liuhui2"
});
var obj3 = Object.create({
    name: "liuhui3"
});

2.工廠模式

// 優(yōu)點(diǎn):改善了字面量方法創(chuàng)建多個相似對象的問題,不需要編寫重復(fù)的代碼
// 缺點(diǎn):沒有解決對象是別的問題,不知道對象的類型是什么
function person () {
    var obj = new Object();
    obj.name = "liuhui4";
    obj.sayName = function () {
      console.log(this.name);
    };
    return obj;
}
var obj4 = person();
console.log(obj4.sayName());

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

// 優(yōu)點(diǎn):創(chuàng)建并定義了對象類型的屬性和方法,可以標(biāo)示為特定的類型(自定義構(gòu)造函數(shù),原生:Object/Array)
// 缺點(diǎn):方法沒有被共享,每次實(shí)例一個對象都要重復(fù)綁定一個獨(dú)立的方法
function Person (name) {
    this.name = name;
    this.sayName = function () {
        console.log(this.name);
    };
    this.newFun = newFun;
}
// 解決辦法:將方法寫在全局,但是就變成全局方法了跟封裝的觀念相違背了
// 新增代碼
function newFun () {
    console.log("new name: " + this.name);
}
var obj5 = new Person("liuhui5");
console.log(obj5.sayName());

4.原型模式

// 優(yōu)點(diǎn):將方法封裝到相應(yīng)的原型對象上,私有并實(shí)例可以共享這些屬性和方法
// 缺點(diǎn):所有屬性和方法都共享了,改變實(shí)例的屬性和方法會影響原型對象上的屬性和方法
function Person1 () {

}
Person1.prototype = {
    constructor: Person1,
    name: "liuhui6",
    sayName: function () {
        console.log(this.name);
    }
};
var obj6 = new Person1();
console.log(obj6.sayName());

5.組合模式(構(gòu)造函數(shù)+原型模式)還沒有完全理解

// 優(yōu)點(diǎn):構(gòu)造函數(shù)用于定義實(shí)例屬性和方法,原型模式用于共享定義的屬性和方法
function Person2 (name) {
    this.newName = function () {
        console.log(name);
    }
}
Person2.prototype = {
    constructor: Person2,
    name: "liuhui7",
    sayName: function () {
        console.log(this.name);
    }
};
var obj7 = new Person2();
var obj8 = new Person2("liuhui8");
繼承的方法

1.借助構(gòu)造函數(shù)實(shí)現(xiàn)繼承

// 缺點(diǎn):父級構(gòu)造函數(shù)屬性和方法沒有被共享
function Parent1 () {
    this.name = "parent1";
    this.play = [1, 2, 3];
}
function Child1 () {
    Parent1.call(this); // 關(guān)鍵:執(zhí)行父級構(gòu)造函數(shù),深復(fù)制
    this.type = "child1";
}
Parent1.prototype.say = function () {
    console.log("hello1");
};
console.log(new Child1());
var s1 = new Child1();
var s2 = new Child1();
s1.play.push(4);
console.log(s1.play, s2.play, s1.hasOwnProperty("play"), "test1");

2.借助原型鏈實(shí)現(xiàn)繼承

// 缺點(diǎn):原型鏈上的屬性和方法是共享
function Parent2 () {
    this.name = "parent2";
    this.play = [1, 2, 3];
}
function Child2 () {
    this.type = "child";
}
Parent2.prototype.say = function () {
    console.log("hello2");
};
Child2.prototype = new Parent2(); // 關(guān)鍵:將子級執(zhí)行父級構(gòu)造函數(shù)實(shí)現(xiàn)繼承,淺復(fù)制
console.log(new Child2(), new Child2().say());
var s3 = new Child2();
var s4 = new Child2();
s3.play.push(4);
console.log(s3.play, s4.play, s3.hasOwnProperty("play"), "test2");

3.組合方式

// 缺點(diǎn):父級構(gòu)造函數(shù)執(zhí)行了兩遍
function Parent3 () {
    this.name = "parent3";
    this.play = [1, 2, 3];
}
function Child3 () {
    Parent3.call(this); // 關(guān)鍵1
    this.type = "child3";
}
Parent3.prototype.say = function () {
    console.log("hello3");
};
Child3.prototype = new Parent3(); // 關(guān)鍵2
var s5 = new Child3();
var s6 = new Child3();
s5.play.push(4);
console.log(s5, s6, s5.hasOwnProperty("play"), "test");

4.組合方式優(yōu)化1

// 缺點(diǎn):實(shí)例屬性constructor指向父級構(gòu)造函數(shù),使用instanceof不能正確判斷對象類型
function Parent4 () {
    this.name = "parent4";
    this.play = [1, 2, 3];
}
function Child4 () {
    Parent4.call(this);
    this.type = "child4";
}
Parent4.prototype.say = function () {
    console.log("hello4");
};
Child4.prototype = Parent4.prototype;
Child4.prototype.constructor = Child4; // 新增代碼,讓實(shí)例指向其真實(shí)的構(gòu)造函數(shù)
var s7 = new Child4();
var s8 = new Child4();
s7.play.push(4);
console.log(s7, s8);
// 判斷是子原型Child4實(shí)例化還是Parent4直接實(shí)例化的
console.log(s7 instanceof Child4, s8 instanceof Parent4); // 判斷不了
console.log(s7.constructor, s8.constructor, s7.hasOwnProperty("play"), "test");

5.組合方式優(yōu)化2

function Parent5 () {
    this.name = "parent5";
    this.play = [1, 2, 3];
}
function Child5 () {
    Parent5.call(this);
    this.type = "child5";
}
Parent5.prototype.say = function () {
    console.log("hello5");
};
Child5.prototype = Object.create(Parent5.prototype);
Child5.prototype.constructor = Child5;
var s9 = new Child5();
var s10 = new Child5();
s9.play.push(4);
console.log(s9 instanceof Child5, s9 instanceof Parent5);
console.log(s9.constructor, s10.constructor, s9.hasOwnProperty("play"), "test11");
參考

構(gòu)造函數(shù)的繼承

非構(gòu)造函數(shù)的繼承

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/88631.html

相關(guān)文章

  • JS面向對象之一 【概述】

    摘要:更形象的我們還可以將面向?qū)ο罄斫鉃橐环N宗教信仰。這就導(dǎo)致面向?qū)ο蠼痰某绦騿T們在寫時就很難受。所以為了滿足信仰面向?qū)ο蠼痰男枨笸ㄟ^構(gòu)造函數(shù)的形式模擬了偽類。這個套路的核心就是類那么里沒有類所以其實(shí)是通過構(gòu)造函數(shù)來模擬的偽類。 JS面向?qū)ο笾?【概述】 在學(xué)習(xí)JS的面向?qū)ο笾?我們應(yīng)該先自問這樣幾個問題: 面向?qū)ο笫鞘裁匆馑? 學(xué)習(xí)面向?qū)ο蟮暮诵氖鞘裁? 為什么要學(xué)習(xí)面向?qū)ο?(它的...

    JohnLui 評論0 收藏0
  • 面向對象的 JavaScript

    摘要:是完全的面向?qū)ο笳Z言,它們通過類的形式組織函數(shù)和變量,使之不能脫離對象存在。而在基于原型的面向?qū)ο蠓绞街?,對象則是依靠構(gòu)造器利用原型構(gòu)造出來的。 JavaScript 函數(shù)式腳本語言特性以及其看似隨意的編寫風(fēng)格,導(dǎo)致長期以來人們對這一門語言的誤解,即認(rèn)為 JavaScript 不是一門面向?qū)ο蟮恼Z言,或者只是部分具備一些面向?qū)ο蟮奶卣?。本文將回歸面向?qū)ο蟊疽?,從對語言感悟的角度闡述為什...

    novo 評論0 收藏0
  • 體驗(yàn)javascript之美6:如果你覺得什么都會了或者不知道js學(xué)什么了看這里-面向對象編程

    摘要:面向過程函數(shù)式編程面向?qū)ο缶幊痰诙€并不是大家理解的那樣,我們先說舉個現(xiàn)實(shí)例子就明白了。多說一句函數(shù)是編程是非常強(qiáng)大也是我最喜歡的,以后再說,我們先說面向?qū)ο缶幊獭? 概述 當(dāng)大家已經(jīng)把js的語言基礎(chǔ)理解了,然后能夠?qū)懗鲆恍┖唵蔚睦恿?,這個時候基本上達(dá)到了一年工作經(jīng)驗(yàn)的水平,而自己能夠獨(dú)立的寫一些小功能,完成一些小效果,或者臨摹修改一些比較復(fù)雜的插件的時候差不多就是兩年工作經(jīng)驗(yàn)的水平,...

    changfeng1050 評論0 收藏0
  • JS對象(1)重新認(rèn)識面向對象

    摘要:對象重新認(rèn)識面向?qū)ο竺嫦驅(qū)ο髲脑O(shè)計(jì)模式上看,對象是計(jì)算機(jī)抽象現(xiàn)實(shí)世界的一種方式。除了字面式聲明方式之外,允許通過構(gòu)造器創(chuàng)建對象。每個構(gòu)造器實(shí)際上是一個函數(shù)對象該函數(shù)對象含有一個屬性用于實(shí)現(xiàn)基于原型的繼承和共享屬性。 title: JS對象(1)重新認(rèn)識面向?qū)ο? date: 2016-10-05 tags: JavaScript 0x00 面向?qū)ο?從設(shè)計(jì)模式上看,對象是...

    superw 評論0 收藏0
  • JS面向對象一:MVC的面向對象封裝

    摘要:自己的理解的第一個參數(shù)就是的值如果沒用默認(rèn)是那個調(diào)用函數(shù)的當(dāng)前的對象在全局作用域中就是被隱藏的所以不寫且在全局作用于調(diào)用函數(shù)的時候就是可以使用或者自己指定的指向 JS面向?qū)ο笠?MVC的面向?qū)ο蠓庋b MDNjavascript面向?qū)ο?面向?qū)ο?Object-Oriented) showImg(https://segmentfault.com/img/remote/1460000016...

    Scliang 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<