javascript設(shè)計模式之對象工廠函數(shù)與構(gòu)造函數(shù)詳解
下面通過文字詳解加代碼分析的方式給大家分享下javascript設(shè)計模式之對象工廠函數(shù)與構(gòu)造函數(shù)的相關(guān)知識。
概述使用對象字面量,或者向空對象中動態(tài)地添加新成員,是最簡單易用的對象創(chuàng)建方法。然而,除了這兩種常用的對象創(chuàng)建方式,JavaScript還提供了其他方法創(chuàng)建對象。1).使用工廠函數(shù)創(chuàng)建對象我們可以編寫一個函數(shù),此函數(shù)的功能就是創(chuàng)建對象,可將其.
概述
使用對象字面量,或者向空對象中動態(tài)地添加新成員,是最簡單易用的對象創(chuàng)建方法。
然而,除了這兩種常用的對象創(chuàng)建方式,JavaScript還提供了其他方法創(chuàng)建對象。
1).使用工廠函數(shù)創(chuàng)建對象
我們可以編寫一個函數(shù),此函數(shù)的功能就是創(chuàng)建對象,可將其稱為“對象工廠方法”。
//工廠函數(shù)
function createPerson(name, age, job) {
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function () {
console.info(this.name);
};
return o; } //使用工廠函數(shù)創(chuàng)建對象
var person1 = createPerson('張三', 29, '軟件工程師');
var person2 = createPerson('李四', 40, '醫(yī)生');
2).定義對象構(gòu)造函數(shù)
a).對象構(gòu)造函數(shù)首字母大寫
b).內(nèi)部使用this關(guān)鍵字給對象添加成員
c).使用new關(guān)鍵字調(diào)用對象構(gòu)造函數(shù)
//定義對象“構(gòu)造”函數(shù)
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = function () {
console.info(this.name);
};
} //使用new調(diào)用對象構(gòu)造函數(shù)創(chuàng)建對象
var p1 = new Person('張三', 29, '軟件工程師');
var p2 = new Person('李四', 40, '醫(yī)生');
以普通方式調(diào)用的“構(gòu)造函數(shù)”
構(gòu)造函數(shù)其實(shí)也是一個函數(shù),不同之處在于調(diào)用它時必須要加一個“new”關(guān)鍵字,如果不加這個關(guān)鍵字,則對它的調(diào)用被認(rèn)為是普通函數(shù)調(diào)用。
//作為普通函數(shù)調(diào)用的構(gòu)造函數(shù),通過this添加的屬性,
//成為了window對象的屬性與方法。
console.info(window.name);//張三
console.info(window.age); //29
console.info(window.job); //軟件工程師
對象構(gòu)造函數(shù)長得這個樣:
function Person (name) {
this.name = name;
this.say = function () {
return "I am " + this.name;
};
}
實(shí)際上是這樣(示意):
function Person (name) {
// var this = {};
this.name = name;
this.say = function () {
return "I am " + this.name;
};
// return this;
}
構(gòu)造函數(shù)完成的工作
1. 創(chuàng)建一個新的對象
2. 讓構(gòu)造函數(shù)的this引用這一新創(chuàng)建的對象
3. 執(zhí)行構(gòu)造函數(shù)中的代碼,這些代碼通常完成向新對象添加屬性的工作
4. 向外界返回新創(chuàng)建的對象引用。
對象構(gòu)造函數(shù)與對象工廠方法的區(qū)別
1. 對象構(gòu)造函數(shù)中沒有顯式的對象創(chuàng)建代碼
2. 新對象應(yīng)具備的屬性與方法是通過this引用添加的.
3. 對象構(gòu)造函數(shù)中沒有return語句
通常會把對象構(gòu)造函數(shù)的首字母設(shè)置為大寫的,以區(qū)別于普通函數(shù)。
對象的constructor屬性
a).使用對象工廠函數(shù)創(chuàng)建對象,每個對象的constructor屬性引用Object()
var person = createPerson('張三', 29, '軟件工程師');
//使用工廠方法創(chuàng)建對象,
其constructor屬性引用Object()函數(shù)
console.info(person1.constructor === Object);
//true
b).使用對象構(gòu)造函數(shù)創(chuàng)建對象,每個對象的constructor屬性引用這個構(gòu)造函數(shù)
var p = new Person('張三', 29, '軟件工程師');
//使用對象構(gòu)造函數(shù)創(chuàng)建對象,
//每個對象的constructor屬性,引用這個構(gòu)造函數(shù)
console.info(p.constructor === Person);
//true如何避免“忘記”new?可以使用arguments.callee解決這個問題
//了解arguments.callee的作用
function TestArgumentsCallee()
{
console.info(this);
console.info(this instanceof TestArgumentsCallee);
console.info(this instanceof arguments.callee);
};
TestArgumentsCallee(); //window
//false
//false
new TestArgumentsCallee();
//TestArgumentsCallee
//true
//true
于是,可以直接用arguments.callee
//避免忘記new
function MyObject(value)
{
if (!(this instanceof arguments.callee))
{
//如果調(diào)用者忘記加上new了,就加上new再調(diào)用一次
return new MyObject(value);
}
this.prop = value;
}
//測試
var obj1 = new MyObject(100);
console.info(obj1.prop);//100
var obj2 = MyObject(200);
console.info(obj2.prop); //200
以上內(nèi)容就是javascript設(shè)計模式之對象工廠函數(shù)與構(gòu)造函數(shù)詳解,希望大家喜歡。
- 詳解Javascript 中的 class、構(gòu)造函數(shù)、工廠函數(shù)
- JavaScript構(gòu)造函數(shù)詳解
- JS特殊函數(shù)(Function()構(gòu)造函數(shù)、函數(shù)直接量)區(qū)別介紹
- JS面向?qū)ο蠡A(chǔ)講解(工廠模式、構(gòu)造函數(shù)模式、原型模式、混合模式、動態(tài)原型模式)
- JS中的構(gòu)造函數(shù)詳細(xì)解析
- 深入理解javascript構(gòu)造函數(shù)和原型對象
- JavaScript中的工廠函數(shù)(推薦)
- JavaScript中工廠函數(shù)與構(gòu)造函數(shù)示例詳解
相關(guān)文章
舉例說明如何為JavaScript的方法參數(shù)設(shè)置默認(rèn)值
這篇文章主要介紹了舉例說明如何為JavaScript的方法參數(shù)設(shè)置默認(rèn)值,參數(shù)默認(rèn)值的設(shè)置是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-11-11javascript函數(shù)中的arguments參數(shù)
arguments當(dāng)然只在function體內(nèi)才有意義, arguments.length 返回的是傳入function的實(shí)參個數(shù)2010-08-08