介紹
建構函式大家都很熟悉了,不過如果你是新手,還是有必要來瞭解一下什麼叫建構函式的。建構函式用於建立特定類型的對象——不僅聲明了使用的對象,構 造函數還可以接受參數以便第一次建立對象的時候設定對象的成員值。你可以自訂自己的建構函式,然後在裡面聲明自訂類型對象的屬性或方法。
基本用法
在JavaScript裡,建構函式通常是認為用來實現執行個體的,JavaScript沒有類的概念,但是有特殊的建構函式。通過new關鍵字來調用 定義的否早函數,你可以告訴JavaScript你要建立一個新對象並且新對象的成員聲明都是建構函式裡定義的。在建構函式內部,this關鍵字引用的是 新建立的對象。基本用法如下:
function Car(model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
this.output= function () {
return this.model + "走了" + this.miles + "公裡";
};
}
var tom= new Car("大叔", 2009, 20000);
var dudu= new Car("Dudu", 2010, 5000);
console.log(tom.output());
console.log(dudu.output());
上面的例子是個非常簡單的建構函式模式,但是有點小問題。首先是使用繼承很麻煩了,其次output()在每次建立對象的時候都重新定義了,最好的方法是讓所有Car類型的執行個體都共用這個output()方法,這樣如果有大批量的執行個體的話,就會節約很多記憶體。
解決這個問題,我們可以使用如下方式:
function Car(model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
this.output= formatCar;
}
function formatCar() {
return this.model + "走了" + this.miles + "公裡";
}
這個方式雖然可用,但是我們有如下更好的方式。
建構函式與原型
JavaScript裡函數有個原型屬性叫prototype,當調用建構函式建立對象的時候,所有該建構函式原型的屬性在新建立對象上都可用。按照這樣,多個Car對象執行個體可以共用同一個原型,我們再擴充一下上例的代碼:
function Car(model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
}
/*
注意:這裡我們使用了Object.prototype.方法名,而不是Object.prototype
主要是用來避免重寫定義原型prototype對象
*/
Car.prototype.output= function () {
return this.model + "走了" + this.miles + "公裡";
};
var tom = new Car("大叔", 2009, 20000);
var dudu = new Car("Dudu", 2010, 5000);
console.log(tom.output());
console.log(dudu.output());
這裡,output()單一實例可以在所有Car對象執行個體裡共用使用。
另外:我們推薦建構函式以大寫字母開頭,以便區分普通的函數。
只能用new嗎?
上面的例子對函數car都是用new來建立對象的,只有這一種方式嗎?其實還有別的方式,我們列舉兩種:
function Car(model, year, miles) {
this.model = model;
this.year = year;
this.miles = miles;
// 自訂一個output輸出內容
this.output = function () {
return this.model + "走了" + this.miles + "公裡";
}
}
//方法1:作為函數調用
Car("大叔", 2009, 20000); //添加到window對象上
console.log(window.output());
//方法2:在另外一個對象的範圍內調用
var o = new Object();
Car.call(o, "Dudu", 2010, 5000);
console.log(o.output());
該代碼的方法1有點特殊,如果不適用new直接調用函數的話,this指向的是全域對象window,我們來驗證一下:
//作為函數調用
var tom = Car("大叔", 2009, 20000);
console.log(typeof tom); // "undefined"
console.log(window.output()); // "大叔走了20000公裡"
這時候對象tom是undefined,而window.output()會正確輸出結果,而如果使用new關鍵字則沒有這個問題,驗證如下:
//使用new 關鍵字
var tom = new Car("大叔", 2009, 20000);
console.log(typeof tom); // "object"
console.log(tom.output()); // "大叔走了20000公裡"
強制使用new
上述的例子展示了不使用new的問題,那麼我們有沒有辦法讓建構函式強制使用new關鍵字呢,答案是肯定的,上代碼:
function Car(model, year, miles) {
if (!(this instanceof Car)) {
return new Car(model, year, miles);
}
this.model = model;
this.year = year;
this.miles = miles;
this.output = function () {
return this.model + "走了" + this.miles + "公裡";
}
}
var tom = new Car("大叔", 2009, 20000);
var dudu = Car("Dudu", 2010, 5000);
console.log(typeof tom); // "object"
console.log(tom.output()); // "大叔走了20000公裡"
console.log(typeof dudu); // "object"
console.log(dudu.output()); // "Dudu走了5000公裡"
通過判斷this的instanceof是不是Car來決定返回new Car還是繼續執行代碼,如果使用的是new關鍵字,則(this instanceof Car)為真,會繼續執行下面的參數賦值,如果沒有用new,(this instanceof Car)就為假,就會重新new一個執行個體返回。
原始封裝函數
JavaScript裡有3中原始封裝函數:number, string, boolean,有時候兩種都用:
// 使用原始封裝函數
var s = new String("my string");
var n = new Number(101);
var b = new Boolean(true);
// 推薦這種
var s = "my string";
var n = 101;
var b = true;
推薦,只有在想保留數值狀態的時候使用這些封裝函數,關於區別可以參考下面的代碼:
// 原始string
var greet = "Hello there";
// 使用split()方法分割
greet.split(' ')[0]; // "Hello"
// 給原始類型添加新屬性不會報錯
greet.smile = true;
// 單沒法擷取這個值(18章ECMAScript實現裡我們講了為什麼)
console.log(typeof greet.smile); // "undefined"
// 原始string
var greet = new String("Hello there");
// 使用split()方法分割
greet.split(' ')[0]; // "Hello"
// 給封裝函數類型添加新屬性不會報錯
greet.smile = true;
// 可以正常訪問新屬性
console.log(typeof greet.smile); // "boolean"
總結
本章主要講解了建構函式模式的使用方法、調用方法以及new關鍵字的區別,希望大家在使用的時候有所注意。
參考:http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#constructorpatternjavascript
轉自:湯姆大叔