首頁  >  文章  >  web前端  >  JavaScript程式碼復用模式實例分析_javascript技巧

JavaScript程式碼復用模式實例分析_javascript技巧

WBOY
WBOY原創
2016-05-16 17:47:39956瀏覽

任何程式設計都提出程式碼重複使用,否則話每次開發一個新程式或寫一個新功能都要全新編寫的話,那就歇菜了,但是程式碼複用也是有好要壞,接下來的兩篇文章我們將針對程式碼重複使用來進行討論,第一篇文避免篇,指的是要盡量避免使用這些模式,因為或多或少有帶來一些問題;第二排是推薦篇,指的是推薦大家使用的模式,一般不會有問題。

模式1:預設模式
程式碼復用大家常用的預設模式,往往是有問題的,該模式使用Parent()的建構子建立一個對象,並且將此物件賦值給Child()的原型。我們來看看程式碼:

複製程式碼 程式碼如下:

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


模式2:借用建構子
此模式是Child借用Parent的建構子進行apply,然後將child的this和參數傳遞給apply方法:
複製程式碼 程式碼如下:

// 父建構子function Parent(name) {function Parent(name) this.name = name || 'Adam';}
// 為原型加入say功能Parent.prototype.say = function () { return this.name;};
// Child建構子function Child( name) { Parent.apply(this, arguments);}var kid = new Child("Patrick");console.log(kid.name);
// "Patrick"// 缺點:沒有從建構子上繼承say方法console.log(typeof kid.say);
// "undefined"缺點也很明顯,say方法不可用,因為沒有繼承過來。


模式3:借用構造函數並設定原型
上述兩個模式都有自己的缺點,那如何把兩者的缺點去除呢,讓我們來試試看:
// 父建構子function Parent(name) { this.name = name || 'Adam';}// 為原型加上say功能Parent.prototype.say = function () { return this .name;};// Child建構子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"運行起來,一切正常,但是有沒有發現,Parent建構函式執行了兩次,所以說,雖然程式可用,但是效率很低。

模式4:共享原型
共享原型是指Child和Parent使用同樣的原型,程式碼如下:
複製程式碼 程式碼如下:

function inherit(C, P) { C.prototype = P.prototype;}
// 父建構子function Parent (name) { this.name = name || 'Adam';}
// 給原型添加say功能Parent.prototype.say = function () { return this.name;};
// Child構造函數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的參數沒有正確接收。


模式5:臨時建構子
先借用建構函數,然後將Child的原型設定為該借用建構函數的實例,最後恢復Child原型的構造函數。程式碼如下:
複製程式碼 程式碼如下:

/* 閉包*/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建構子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不能正常接收參數。

模式6:klass
這個模式,先上程式碼:
var klass = function (Parent, props) { var Child, F, i; //
1. // 新建構子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. // 新增實作方法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);

看著是不是有點暈,說好點,該模式的語法和規範擰得和別的語言一樣,你願意用麼?

總結
以上六個模式雖然在某種特殊情況下實現了某些功能,但是都存在各自的缺點,所以一般情況,大家要避免使用。
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn