介紹
本篇主要是介紹創建對象方面的模式的下篇,利用各種技巧可以極大地避免了錯誤或者可以編寫出非常精簡的代碼。
模式6:函數語法糖
函數語法糖是為一個對象快速添加方法(函數)的擴展,這個主要是利用prototype的特性,代碼比較簡單,我們先來看一下實現代碼:
模式7:對象常量
對象常量是在一個對象提供set,get,ifDefined各種方法的體現,而且對于set的方法只會保留最先設置的對象,后期再設置都是無效的,已達到別人無法重載的目的。實現代碼如下:
return {
// 設置名稱為name的屬性
set: function (name, value) {
if (this.isDefined(name)) {
return false;
}
if (!ownProp.call(allowed, typeof value)) {
return false;
}
constants[prefix + name] = value;
return true;
},
// 判斷是否存在名稱為name的屬性
isDefined: function (name) {
return ownProp.call(constants, prefix + name);
},
// 獲取名稱為name的屬性
get: function (name) {
if (this.isDefined(name)) {
return constants[prefix + name];
}
return null;
}
};
} ());
驗證代碼如下:
// 定義
console.log(constant.set("maxwidth", 480)); // true
// 重新檢測
console.log(constant.isDefined("maxwidth")); // true
// 嘗試重新定義
console.log(constant.set("maxwidth", 320)); // false
// 判斷原先的定義是否還存在
console.log(constant.get("maxwidth")); // 480
模式8:沙盒模式
沙盒(Sandbox)模式即時為一個或多個模塊提供單獨的上下文環境,而不會影響其他模塊的上下文環境,比如有個Sandbox里有3個方法event,dom,ajax,在調用其中2個組成一個環境的話,和調用三個組成的環境完全沒有干擾。Sandbox實現代碼如下:
// 強制使用new操作符
if (!(this instanceof Sandbox)) {
return new Sandbox(modules, callback);
}
// 添加屬性
this.a = 1;
this.b = 2;
// 向this對象上需想添加模塊
// 如果沒有模塊或傳入的參數為 "*" ,則以為著傳入所有模塊
if (!modules || modules == '*') {
modules = [];
for (i in Sandbox.modules) {
if (Sandbox.modules.hasOwnProperty(i)) {
modules.push(i);
}
}
}
// 初始化需要的模塊
for (i = 0; i < modules.length; i += 1) {
Sandbox.modules[modules[i]](this);
}
// 調用 callback
callback(this);
}
// 默認添加原型對象
Sandbox.prototype = {
name: "My Application",
version: "1.0",
getName: function () {
return this.name;
}
};
然后我們再定義默認的初始模塊:
Sandbox.modules.dom = function (box) {
box.getElement = function () {
};
box.getStyle = function () {
};
box.foo = "bar";
};
Sandbox.modules.event = function (box) {
// access to the Sandbox prototype if needed:
// box.constructor.prototype.m = "mmm";
box.attachEvent = function () {
};
box.detachEvent = function () {
};
};
Sandbox.modules.ajax = function (box) {
box.makeRequest = function () {
};
box.getResponse = function () {
};
};
調用方式如下:
Sandbox('ajax', 'dom', function (box) {
console.log(typeof (box.attachEvent));
// 沒有選擇event,所以event里定義的attachEvent也不存在
});
Sandbox('*', function (box) {
console.log(box); // 上面定義的所有方法都可訪問
});
模式9:靜態成員
靜態成員(Static Members)只是一個函數或對象提供的靜態屬性,可分為私有的和公有的,就像C#或Java里的public static和private static一樣。
我們先來看一下公有成員,公有成員非常簡單,我們平時聲明的方法,函數都是公有的,比如:
// 公有靜態方法
Gadget.isShiny = function () {
return "you bet";
};
// 原型上添加的正常方法
Gadget.prototype.setPrice = function (price) {
this.price = price;
};
// 調用靜態方法
console.log(Gadget.isShiny()); // "you bet"
// 創建實例,然后調用方法
var iphone = new Gadget();
iphone.setPrice(500);
console.log(typeof Gadget.setPrice); // "undefined"
console.log(typeof iphone.isShiny); // "undefined"
Gadget.prototype.isShiny = Gadget.isShiny;
console.log(iphone.isShiny()); // "you bet"
而私有靜態成員,我們可以利用其閉包特性去實現,以下是兩種實現方式。
第一種實現方式:
// 閉包返回構造函數的新實現
return function () {
console.log(counter += 1);
};
} ()); // 立即執行
var g1 = new Gadget(); // logs 1
var g2 = new Gadget(); // logs 2
var g3 = new Gadget(); // logs 3
第二種方式:
//新構造函數實現
NewGadget = function () {
counter += 1;
};
// 授權可以訪問的方法
NewGadget.prototype.getLastId = function () {
return counter;
};
// 覆蓋構造函數
return NewGadget;
} ()); // 立即執行
var iphone = new Gadget();
iphone.getLastId(); // 1
var ipod = new Gadget();
ipod.getLastId(); // 2
var ipad = new Gadget();
ipad.getLastId(); // 3
總結
這是對象創建模式的下篇,兩篇一起總共9種模式,是我們在日常JavaScript編程中經常使用的對象創建模式,不同的場景起到了不同的作用,希望大家根據各自的需求選擇適用的模式。
新聞熱點
疑難解答
圖片精選