最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript代碼復(fù)用模式實(shí)例分析

 更新時(shí)間:2012年12月02日 16:09:27   作者:  
任何編程都提出代碼復(fù)用,否則話每次開發(fā)一個(gè)新程序或者寫一個(gè)新功能都要全新編寫的話,效率太差了,接下來我們將針對代碼復(fù)用來進(jìn)行討論,需要的朋友可以參考下

任何編程都提出代碼復(fù)用,否則話每次開發(fā)一個(gè)新程序或者寫一個(gè)新功能都要全新編寫的話,那就歇菜了,但是代碼復(fù)用也是有好要壞,接下來的兩篇文章我們將針對代碼復(fù)用來進(jìn)行討論,第一篇文避免篇,指的是要盡量避免使用這些模式,因?yàn)榛蚨嗷蛏儆袔硪恍﹩栴};第二排是推薦篇,指的是推薦大家使用的模式,一般不會(huì)有什么問題。

模式1:默認(rèn)模式
代碼復(fù)用大家常用的默認(rèn)模式,往往是有問題的,該模式使用Parent()的構(gòu)造函數(shù)創(chuàng)建一個(gè)對象,并且將該對象賦值給Child()的原型。我們看一下代碼:

復(fù)制代碼 代碼如下:

function inherit(C, P) { C.prototype = new P();}
// 父構(gòu)造函數(shù)function Parent(name) { this.name = name || 'Adam';}
// 給原型添加say功能Parent.prototype.say = function () { return this.name;};
// Child構(gòu)造函數(shù)為空function Child(name) {}
// 執(zhí)行繼承inherit(Child, Parent);var kid = new Child();console.log(kid.say());
// "Adam"var kiddo = new Child();kiddo.name = "Patrick";console.log(kiddo.say());
// "Patrick"http:// 缺點(diǎn):不能讓參數(shù)傳進(jìn)給Child構(gòu)造函數(shù)var s = new Child('Seth');console.log(s.say());
// "Adam"這種模式的缺點(diǎn)是Child不能傳進(jìn)參數(shù),基本上也就廢了。


模式2:借用構(gòu)造函數(shù)
該模式是Child借用Parent的構(gòu)造函數(shù)進(jìn)行apply,然后將child的this和參數(shù)傳遞給apply方法:
復(fù)制代碼 代碼如下:

// 父構(gòu)造函數(shù)function Parent(name) { this.name = name || 'Adam';}
// 給原型添加say功能Parent.prototype.say = function () { return this.name;};
// Child構(gòu)造函數(shù)function Child(name) { Parent.apply(this, arguments);}var kid = new Child("Patrick");console.log(kid.name);
// "Patrick"http:// 缺點(diǎn):沒有從構(gòu)造函數(shù)上繼承say方法console.log(typeof kid.say);
// "undefined"缺點(diǎn)也很明顯,say方法不可用,因?yàn)闆]有繼承過來。


模式3:借用構(gòu)造函數(shù)并設(shè)置原型
上述兩個(gè)模式都有自己的缺點(diǎn),那如何把兩者的缺點(diǎn)去除呢,我們來嘗試一下:
// 父構(gòu)造函數(shù)function Parent(name) { this.name = name || 'Adam';}// 給原型添加say功能Parent.prototype.say = function () { return this.name;};// Child構(gòu)造函數(shù)function Child(name) { Parent.apply(this, arguments);}Child.prototype = new Parent();var kid = new Child("Patrick");console.log(kid.name); // "Patrick"console.log(typeof kid.say); // functionconsole.log(kid.say()); // Patrickconsole.dir(kid);delete kid.name;console.log(kid.say()); // "Adam"運(yùn)行起來,一切正常,但是有沒有發(fā)現(xiàn),Parent構(gòu)造函數(shù)執(zhí)行了兩次,所以說,雖然程序可用,但是效率很低。

模式4:共享原型
共享原型是指Child和Parent使用同樣的原型,代碼如下:
復(fù)制代碼 代碼如下:

function inherit(C, P) { C.prototype = P.prototype;}
// 父構(gòu)造函數(shù)function Parent(name) { this.name = name || 'Adam';}
// 給原型添加say功能Parent.prototype.say = function () { return this.name;};
// Child構(gòu)造函數(shù)function Child(name) {}inherit(Child, Parent);var kid = new Child('Patrick');console.log(kid.name);
// undefinedconsole.log(typeof kid.say);
// functionkid.name = 'Patrick';console.log(kid.say());
// Patrickconsole.dir(kid);確定還是一樣,Child的參數(shù)沒有正確接收到。


模式5:臨時(shí)構(gòu)造函數(shù)
首先借用構(gòu)造函數(shù),然后將Child的原型設(shè)置為該借用構(gòu)造函數(shù)的實(shí)例,最后恢復(fù)Child原型的構(gòu)造函數(shù)。代碼如下:
復(fù)制代碼 代碼如下:

/* 閉包 */var inherit = (function () { var F = function () { }; return function (C, P) { F.prototype = P.prototype; C.prototype = new F(); C.uber = P.prototype; CC.prototype.constructor = C; }} ());function Parent(name) { this.name = name || 'Adam';}// 給原型添加say功能Parent.prototype.say = function () { return this.name;};// Child構(gòu)造函數(shù)function Child(name) {}inherit(Child, Parent);var kid = new Child();console.log(kid.name); // undefinedconsole.log(typeof kid.say); // functionkid.name = 'Patrick';console.log(kid.say()); // Patrickvar kid2 = new Child("Tom");console.log(kid.say()); console.log(kid.constructor.name); // Childconsole.log(kid.constructor === Parent); // false問題照舊,Child不能正常接收參數(shù)。

模式6:klass
這個(gè)模式,先上代碼吧:
var klass = function (Parent, props) { var Child, F, i; //
1. // 新構(gòu)造函數(shù) Child = function () { if (Child.uber && Child.uber.hasOwnProperty("__construct")) { Child.uber.__construct.apply(this, arguments); } if (Child.prototype.hasOwnProperty("__construct")) { Child.prototype.__construct.apply(this, arguments); } }; //
2. // 繼承 ParentParent = Parent || Object; F = function () { }; F.prototype = Parent.prototype; Child.prototype = new F(); Child.uber = Parent.prototype; ChildChild.prototype.constructor = Child; /
3. // 添加實(shí)現(xiàn)方法 for (i in props) { if (props.hasOwnProperty(i)) { Child.prototype[i] = props[i]; } }
// return the "class" return Child;};var Man = klass(null, { __construct: function (what) { console.log("Man's constructor"); this.name = what; }, getName: function () { return this.name; }});var first = new Man('Adam');
// logs "Man's constructor"first.getName();
// "Adam"var SuperMan = klass(Man, { __construct: function (what) { console.log("SuperMan's constructor"); }, getName: function () { var name = SuperMan.uber.getName.call(this); return "I am " + name; }});var clark = new SuperMan('Clark Kent');clark.getName();
// "I am Clark Kent"console.log(clark instanceof Man);
// trueconsole.log(clark instanceof SuperMan);

看著是不是有點(diǎn)暈,說好點(diǎn),該模式的語法和規(guī)范擰得和別的語言一樣,你愿意用么?

總結(jié)
以上六個(gè)模式雖然在某種特殊情況下實(shí)現(xiàn)了某些功能,但是都存在各自的缺點(diǎn),所以一般情況,大家要避免使用。

相關(guān)文章

  • JavaScript獲取某一天所在的星期

    JavaScript獲取某一天所在的星期

    我們會(huì)遇到的需求是,獲取今天或者某一天所在星期的開始和結(jié)束日期。今天小編通過實(shí)例代碼給大家分享JavaScript獲取某一天所在的星期,感興趣的朋友跟隨小編一起看看吧
    2019-09-09
  • JavaScript Ajax編程 應(yīng)用篇

    JavaScript Ajax編程 應(yīng)用篇

    這篇文章主要介紹了JavaScript Ajax編程之應(yīng)用篇,感興趣的朋友可以參考一下
    2016-07-07
  • JS+CSS實(shí)現(xiàn)鼠標(biāo)滑過時(shí)動(dòng)態(tài)翻滾的導(dǎo)航條效果

    JS+CSS實(shí)現(xiàn)鼠標(biāo)滑過時(shí)動(dòng)態(tài)翻滾的導(dǎo)航條效果

    這篇文章主要介紹了JS+CSS實(shí)現(xiàn)鼠標(biāo)滑過時(shí)動(dòng)態(tài)翻滾的導(dǎo)航條效果,涉及JavaScript動(dòng)態(tài)設(shè)置css樣式動(dòng)畫過度效果的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • Smartour 讓網(wǎng)頁導(dǎo)覽變得更簡單(推薦)

    Smartour 讓網(wǎng)頁導(dǎo)覽變得更簡單(推薦)

    這篇文章主要介紹了Smartour 讓網(wǎng)頁導(dǎo)覽變得更簡單(推薦),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 對TypeScript庫進(jìn)行單元測試的方法

    對TypeScript庫進(jìn)行單元測試的方法

    這篇文章主要介紹了對TypeScript庫進(jìn)行單元測試的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • IE與FireFox的兼容性問題分析

    IE與FireFox的兼容性問題分析

    IE與FireFox的兼容性問題分析...
    2007-04-04
  • JavaScript canvas實(shí)現(xiàn)文字時(shí)鐘

    JavaScript canvas實(shí)現(xiàn)文字時(shí)鐘

    這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)文字時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • 微信小程序的線程架構(gòu)【推薦】

    微信小程序的線程架構(gòu)【推薦】

    這篇文章主要介紹了微信小程序的線程架構(gòu),每個(gè)小程序包含一個(gè)描述整體程序的app實(shí)例和多個(gè)描述頁面的page,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-05-05
  • layui實(shí)現(xiàn)點(diǎn)擊按鈕給table添加一行

    layui實(shí)現(xiàn)點(diǎn)擊按鈕給table添加一行

    想實(shí)現(xiàn)點(diǎn)擊按鈕在表格添加一行的功能,但發(fā)現(xiàn)layui并未集成該工具欄,因此,需要自己手動(dòng)添加這個(gè)功能;這篇文章主要介紹了layui點(diǎn)擊按鈕給table添加一行,需要的朋友可以參考下
    2018-08-08
  • 微信小程序?qū)崿F(xiàn)登錄注冊功能

    微信小程序?qū)崿F(xiàn)登錄注冊功能

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)登錄注冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12

最新評論

基隆市| 临潭县| 广饶县| 通河县| 九寨沟县| 汨罗市| 高要市| 安徽省| 蓬安县| 凤凰县| 朝阳市| 西丰县| 安国市| 中宁县| 弥勒县| 博罗县| 蒙自县| 新乡市| 宽甸| 水富县| 安仁县| 巴中市| 临海市| 卫辉市| 陈巴尔虎旗| 泰和县| 阜城县| 文山县| 德钦县| 双鸭山市| 镇雄县| 南岸区| 二连浩特市| 拉萨市| 营口市| 彝良县| 德兴市| 临沧市| 垣曲县| 武穴市| 普兰店市|