在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ 代碼復(fù)用模式(避免篇)
代碼復(fù)用模式(避免篇)
S.O.L.I.D 五大原則之接口隔離原則 ISP
設(shè)計模式之狀態(tài)模式
JavaScript 核心(晉級高手必讀篇)
設(shè)計模式之建造者模式
JavaScript 與 DOM(上)——也適用于新手
設(shè)計模式之中介者模式
設(shè)計模式之裝飾者模式
設(shè)計模式之模板方法
設(shè)計模式之外觀模式
強大的原型和原型鏈
設(shè)計模式之構(gòu)造函數(shù)模式
揭秘命名函數(shù)表達式
深入理解J avaScript 系列(結(jié)局篇)
執(zhí)行上下文(Execution Contexts)
函數(shù)(Functions)
《你真懂 JavaScript 嗎?》答案詳解
設(shè)計模式之適配器模式
設(shè)計模式之組合模式
設(shè)計模式之命令模式
S.O.L.I.D 五大原則之單一職責(zé) SRP
編寫高質(zhì)量 JavaScript 代碼的基本要點
求值策略
閉包(Closures)
對象創(chuàng)建模式(上篇)
This? Yes,this!
設(shè)計模式之代理模式
變量對象(Variable Object)
S.O.L.I.D 五大原則之里氏替換原則 LSP
面向?qū)ο缶幊讨话憷碚?/span>
設(shè)計模式之單例模式
Function 模式(上篇)
S.O.L.I.D 五大原則之依賴倒置原則 DIP
設(shè)計模式之迭代器模式
立即調(diào)用的函數(shù)表達式
設(shè)計模式之享元模式
設(shè)計模式之原型模式
根本沒有“JSON 對象”這回事!
JavaScript 與 DOM(下)
面向?qū)ο缶幊讨?ECMAScript 實現(xiàn)
全面解析 Module 模式
對象創(chuàng)建模式(下篇)
設(shè)計模式之職責(zé)鏈模式
S.O.L.I.D 五大原則之開閉原則 OCP
設(shè)計模式之橋接模式
設(shè)計模式之策略模式
設(shè)計模式之觀察者模式
代碼復(fù)用模式(推薦篇)
作用域鏈(Scope Chain)
Function 模式(下篇)
設(shè)計模式之工廠模式

代碼復(fù)用模式(避免篇)

介紹

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

模式 1:默認模式

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

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"
// 缺點:不能讓參數(shù)傳進給Child構(gòu)造函數(shù)
var s = new Child('Seth');
console.log(s.say()); // "Adam"

這種模式的缺點是 Child 不能傳進參數(shù),基本上也就廢了。

模式 2:借用構(gòu)造函數(shù)

該模式是 Child 借用 Parent 的構(gòu)造函數(shù)進行 apply,然后將 child 的 this 和參數(shù)傳遞給 apply 方法:

// 父構(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"
// 缺點:沒有從構(gòu)造函數(shù)上繼承say方法
console.log(typeof kid.say); // "undefined"

缺點也很明顯,say 方法不可用,因為沒有繼承過來。

模式 3:借用構(gòu)造函數(shù)并設(shè)置原型

上述兩個模式都有自己的缺點,那如何把兩者的缺點去除呢,我們來嘗試一下:

// 父構(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); // function
console.log(kid.say()); // Patrick
console.dir(kid);
delete kid.name;
console.log(kid.say()); // "Adam"

運行起來,一切正常,但是有沒有發(fā)現(xiàn),Parent 構(gòu)造函數(shù)執(zhí)行了兩次,所以說,雖然程序可用,但是效率很低。

模式4:共享原型

共享原型是指 Child 和 Parent 使用同樣的原型,代碼如下:

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); // undefined
console.log(typeof kid.say); // function
kid.name = 'Patrick';
console.log(kid.say()); // Patrick
console.dir(kid);

確定還是一樣,Child 的參數(shù)沒有正確接收到。

模式 5:臨時構(gòu)造函數(shù)

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

/* 閉包 */
var inherit = (function () {
    var F = function () {
    };
    return function (C, P) {
        F.prototype = P.prototype;
        C.prototype = new F();
        C.uber = P.prototype;
        C.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); // undefined
console.log(typeof kid.say); // function
kid.name = 'Patrick';
console.log(kid.say()); // Patrick
var kid2 = new Child("Tom");
console.log(kid.say()); 
console.log(kid.constructor.name); // Child
console.log(kid.constructor === Parent); // false

問題照舊,Child 不能正常接收參數(shù)。

模式 6:klass

這個模式,先上代碼吧:

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.
    // 繼承
    Parent = Parent || Object;
    F = function () {
    };
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.uber = Parent.prototype;
    Child.prototype.constructor = Child;  
    // 3.
    // 添加實現(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); // true
console.log(clark instanceof SuperMan); // true

總結(jié)

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