標籤:
[20141227]編寫高品質JS代碼的68個有效方法(八)
NO.36、只將執行個體狀態儲存在執行個體對象中
Tips:
- 共用可變資料可能會出問題,因為原型是被其所有的執行個體共用的
- 將可變的執行個體儲存在執行個體對象中
一般來說,由於原型屬性指向的對象是所有執行個體共用的。所以不建議在原型指向的對象中儲存共用資料。下面給一個簡單的例子:
var Person = function(name){ this.name = name;};Person.prototype = { children: [], addChild: function(childName){ this.children.push(childName); }, getChildren: function(){ return this.children; }};var p1 = new Person(‘P1‘);var p2 = new Person(‘P2‘);p2.addChild(‘P2_C1‘);console.log(p1.getChildren());
結果比較明顯。p2的孩子成p1的了。標準做法是將children儲存在執行個體對象中。
var Person = function(name){ this.name = name; this.children = [];};Person.prototype = { addChild: function(childName){ this.children.push(childName); }, getChildren: function(){ return this.children; }};
No.37、認識到this變數的隱式綁定問題
Tips:
- this變數的範圍總是有其最近的封閉函數所確定
- 使用一個局部變數(通常命名為self,me,that)使得this的綁定對於內建函式是可用的。
老規矩,看一個簡單的樣本:
var testObj = { a1: 0, fun1: function(){ function fun2(){ console.log(this.a1); } fun2(); }};testObj.fun1();
為什麼會這樣呢?因為this變數是以不同的方式被綁定的。每個函數都有一個this變數的隱式綁定。this變數是隱式的綁定到最近的封閉函數。針對以上的問題,可以有集中方法來處理,參考如下:
//通過將this用變數self儲存的方式實現var testObj = { a1: 0, fun1: function(){ var self = this; function fun2(){ console.log(self.a1); } fun2(); }};testObj.fun1();//通過call方法指定接收者(也可以用apply)var testObj = { a1: 0, fun1: function(){ function fun2(){ console.log(this.a1); } fun2.call(this); }};testObj.fun1();//通過bind來實現var testObj = { a1: 1, fun1: function(){ function fun2(){ console.log(this.a1); } fun2.bind(this)(); }};testObj.fun1();
No.38、在子類的建構函式中調用父類的建構函式
Tips:
- 在子類建構函式中顯式地傳入this作為顯式的接收者調用父類的建構函式
- 使用Object.create函數來構造子類的原型對象以避免調用父類的構造
JS中實現的繼承:
var Animal = function(){ this.weight = 50;};Animal.prototype.eat = function(){ console.log(‘eat food...‘);};var Dog = function(){ Animal.call(this); Dog.prototype = Object.create(Animal.prototype);};var dog = new Dog();console.log(dog.weight);
No.39、不要重用父類的屬性名稱
Tips:
- 留意父類使用的所有屬性名稱
- 不要再子類中重用父類的屬性名稱
由於JS中,屬性都是key-value儲存,那麼同名的屬性指向同樣的地址,所以以下代碼:
var Animal = function(){ this.weight = 50; this.id = ++Animal.nextId;};Animal.nextId = 0;Animal.prototype.eat = function(){ console.log(‘eat food...‘);};var Dog = function(){ Animal.call(this); this.id = ++ Dog.nextId; Dog.prototype = Object.create(Animal.prototype);};Dog.nextId = 0;var dog = new Dog();console.log(dog.id);
兩個類都試圖給執行個體屬性id寫資料。
No.40、避免繼承標準類
Tips:
- 繼承標準類往往會由於一些特殊的內部屬性(如[[Class]])而被破壞
- 使用屬性委託優於繼承標準類
擴充標註庫使得其功能更強大是很有誘惑力的,但不幸的是它們的定義具有很多特殊的行為,所以很難寫出正確的子類。
var ArrayEx = function(){ for(var i = 0, len = arguments.length; i<len ; i++){ this[i] = arguments[i]; }};ArrayEx.prototype = Object.create(Array.prototype);var ar = new ArrayEx(‘1‘, ‘2‘);console.log(ar.length) //猜猜結果是什嗎?
原因分析:length屬性只對在內部標記為“真正的”數組對象才起作用。直接繼承的對象並沒有繼承 Array的標記標籤屬性[[Class]]。測試如下:
var ar = new ArrayEx(‘1‘, ‘2‘);console.log(Object.prototype.toString.call(ar)); //[object Object]console.log(Object.prototype.toString.call([])); //[object Array]
ECMAScript標準庫中幹掉大多數建構函式都有類似的問題。基於這個原因,最好避免繼承一下的標準類: Array,Boolean,Date,Function,Number,RegExp或String。
要想實作類別似的功能,可以採用屬性委託的方式:
var ArrayEx = function(){ this.array = [] for(var i = 0, len = arguments.length; i<len ; i++){ this.array[i] = arguments[i]; }};ArrayEx.prototype.forEach = function(f, thisArg){ if(typeof thisArg === ‘undefined‘){ thisArg = this; } this.array.forEach(f, thisArg);};var ar = new ArrayEx(‘1sfdfsd‘, ‘2fdsfs‘);ar.forEach(function(item, i){ console.log(item);});
編寫高品質JS代碼的68個有效方法(八)