從面試題學(xué)習(xí)Javascript 面向?qū)ο螅▌?chuàng)建對(duì)象)
更新時(shí)間:2012年03月30日 00:37:33 作者:
從面試題學(xué)習(xí)Javascript 面向?qū)ο螅▌?chuàng)建對(duì)象),學(xué)習(xí)js的朋友可以參考下
題目:
try{
var me = Man({ fullname: "小紅" });
var she = new Man({ fullname: "小紅" });
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小紅
我的性別是:<用戶未輸入>
------------------*/
me.attr("fullname", "小明");
me.attr("gender", "男");
me.fullname = "廢柴";
me.gender = "人妖";
she.attr("gender", "女");
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小明
我的性別是:男
------------------*/
console.group();
console.info("我的名字是:" + she.attr("fullname") + "\n我的性別是:" + she.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小紅
我的性別是:女
------------------*/
me.attr({
"words-limit": 3,
"words-emote": "微笑"
});
me.words("我喜歡看視頻。");
me.words("我們的辦公室太漂亮了。");
me.words("視頻里美女真多!");
me.words("我平時(shí)都看優(yōu)酷!");
console.group();
console.log(me.say());
/*------[執(zhí)行結(jié)果]------
小明微笑:"我喜歡看視頻。我們的辦公室太漂亮了。視頻里美女真多!"
------------------*/
me.attr({
"words-limit": 2,
"words-emote": "喊"
});
console.log(me.say());
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
小明喊:"我喜歡看視頻。我們的辦公室太漂亮了。"
------------------*/
}catch(e){
console.error("執(zhí)行出錯(cuò),錯(cuò)誤信息: " + e);
}
知識(shí)點(diǎn):
(1)JS面向?qū)ο蠡A(chǔ):ECMA-262把對(duì)象定義為:“無序?qū)傩缘募希鋵傩钥梢园局?、?duì)象或者函數(shù)”。
(2)JS創(chuàng)建對(duì)象的方法:
?。╝)工廠模式:用函數(shù)來封裝以特定接口創(chuàng)建對(duì)象的細(xì)節(jié)。
function createPerson(name, age, job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
alert(this.name);
};
return o;
}
var person1 = createPerson(“Nicholas”, 29, “Software Engineer”);
var person2 = createPerson(“Greg”, 27, “Doctor”);
缺點(diǎn):工廠模式雖然解決了創(chuàng)建多個(gè)相識(shí)對(duì)象的問題,但卻沒有解決對(duì)象識(shí)別的問題(即怎樣知道一個(gè)對(duì)象的類型)。
?。╞)構(gòu)造函數(shù)模式:ECMAScript中的構(gòu)造函數(shù)可以用來創(chuàng)建特定類型的對(duì)象??梢詣?chuàng)建自定義的構(gòu)造函數(shù),從而定義自定義對(duì)象類型的屬性和方法。
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert(this.name);
};
}
var person1 = new Person(“Nicholas”, 29, “Software Engineer”);
var person2 = new Person(“Greg”, 27, “Doctor”);
缺點(diǎn):使用構(gòu)造函數(shù)的主要問題,就是每個(gè)方法都要在每個(gè)實(shí)例上重新創(chuàng)建一遍。不要忘了——ECMAScript中的函數(shù)是對(duì)象,因此每定義一個(gè)函數(shù),
就是實(shí)例化一個(gè)對(duì)象。
?。╟)原型模式:我們創(chuàng)建的每個(gè)函數(shù)都有一個(gè)prototype(原型)屬性,這個(gè)屬性是一個(gè)指針,指向一個(gè)對(duì)象,而這個(gè)對(duì)象的用途是包含可以由特定類型
的所有實(shí)例共享的屬性和方法。使用原型對(duì)象的好處是可以讓所有對(duì)象共享它包含的屬性和方法
function Person(){
}
Person.prototype.name = “Nicholas”;
Person.prototype.age = 29;
Person.prototype.job = “Software Engineer”;
Person.prototype.sayName = function(){
alert(this.name);
};
var person1 = new Person();
person1.sayName(); //”Nicholas”
var person2 = new Person();
person2.sayName(); //”Nicholas”
alert(person1.sayName == person2.sayName); //true
缺點(diǎn):原型中所有屬性是被很多實(shí)例共享的,這種共享對(duì)于函數(shù)非常合適。但是對(duì)于引用類型值的屬性來說,問題就比較突出了。
?。╠)組合使用構(gòu)造函數(shù)模式和原型模式:創(chuàng)建自定義類型的最常見方式,就是使用組合使用構(gòu)造函數(shù)模式和原型模式。構(gòu)造函數(shù)模式用于定義實(shí)例屬性,
而原型模式用于定義方法和共享的屬性。
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.friends = [“Shelby”, “Court”];
}
Person.prototype = {
constructor: Person,
sayName : function () {
alert(this.name);
}
};
var person1 = new Person(“Nicholas”, 29, “Software Engineer”);
var person2 = new Person(“Greg”, 27, “Doctor”);
person1.friends.push(“Van”);
alert(person1.friends); //”Shelby,Court,Van”
alert(person2.friends); //”Shelby,Court”
alert(person1.friends === person2.friends); //false
alert(person1.sayName === person2.sayName); //true
答案:
<!DOCTYPE html>
<html>
<head>
<style type="text/css" rel="stylesheet">
</style>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
window.onload=function()
{
var Man;
//+++++++++++答題區(qū)域+++++++++++
Man=function(obj){
if(!(this instanceof Man))
{
return new Man(obj);
}
this.attrObj=obj||{};
this.wordsObj=[];
}
Man.prototype={
constructor:Man,
words:function(word){
word!=undefined&&this.wordsObj.push(word);
},
attr:function(attribute,attributeValue)
{
var defaultVaule="<用戶未輸入>";
if(arguments.length==2){
this.attrObj[attribute]=attributeValue;
}
else if(!(attribute instanceof Object))
{
if((this.attrObj[attribute]===undefined))
{
return defaultVaule;
}
else
{
return this.attrObj[attribute];
}
}
else{
for(property in attribute)
{
this.attrObj[property]=attribute[property];
}
}
},
say:function()
{
var limit=this.attrObj['words-limit'],
outputString,
wordsLen=this.wordsObj.length;
outputString=this.attr("fullname")+this.attr("words-emote")+":";
for(var i=0;i<limit;i++)
{
outputString+=this.wordsObj[i];
}
return outputString;
}
};
//+++++++++++答題結(jié)束+++++++++++
try{
var me = Man({ fullname: "小紅" });
var she = new Man({ fullname: "小紅" });
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小紅
我的性別是:<用戶未輸入>
------------------*/
me.attr("fullname", "小明");
me.attr("gender", "男");
me.fullname = "廢柴";
me.gender = "人妖";
she.attr("gender", "女");
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小明
我的性別是:男
------------------*/
console.group();
console.info("我的名字是:" + she.attr("fullname") + "\n我的性別是:" + she.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小紅
我的性別是:女
------------------*/
me.attr({
"words-limit": 3,
"words-emote": "微笑"
});
me.words("我喜歡看視頻。");
me.words("我們的辦公室太漂亮了。");
me.words("視頻里美女真多!");
me.words("我平時(shí)都看優(yōu)酷!");
console.group();
console.log(me.say());
/*------[執(zhí)行結(jié)果]------
小明微笑:"我喜歡看視頻。我們的辦公室太漂亮了。視頻里美女真多!"
------------------*/
me.attr({
"words-limit": 2,
"words-emote": "喊"
});
console.log(me.say());
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
小明喊:"我喜歡看視頻。我們的辦公室太漂亮了。"
------------------*/
}catch(e){
console.error("執(zhí)行出錯(cuò),錯(cuò)誤信息: " + e);
}
}
</script>
</html>
復(fù)制代碼 代碼如下:
try{
var me = Man({ fullname: "小紅" });
var she = new Man({ fullname: "小紅" });
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小紅
我的性別是:<用戶未輸入>
------------------*/
me.attr("fullname", "小明");
me.attr("gender", "男");
me.fullname = "廢柴";
me.gender = "人妖";
she.attr("gender", "女");
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小明
我的性別是:男
------------------*/
console.group();
console.info("我的名字是:" + she.attr("fullname") + "\n我的性別是:" + she.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小紅
我的性別是:女
------------------*/
me.attr({
"words-limit": 3,
"words-emote": "微笑"
});
me.words("我喜歡看視頻。");
me.words("我們的辦公室太漂亮了。");
me.words("視頻里美女真多!");
me.words("我平時(shí)都看優(yōu)酷!");
console.group();
console.log(me.say());
/*------[執(zhí)行結(jié)果]------
小明微笑:"我喜歡看視頻。我們的辦公室太漂亮了。視頻里美女真多!"
------------------*/
me.attr({
"words-limit": 2,
"words-emote": "喊"
});
console.log(me.say());
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
小明喊:"我喜歡看視頻。我們的辦公室太漂亮了。"
------------------*/
}catch(e){
console.error("執(zhí)行出錯(cuò),錯(cuò)誤信息: " + e);
}
知識(shí)點(diǎn):
(1)JS面向?qū)ο蠡A(chǔ):ECMA-262把對(duì)象定義為:“無序?qū)傩缘募希鋵傩钥梢园局?、?duì)象或者函數(shù)”。
(2)JS創(chuàng)建對(duì)象的方法:
?。╝)工廠模式:用函數(shù)來封裝以特定接口創(chuàng)建對(duì)象的細(xì)節(jié)。
function createPerson(name, age, job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
alert(this.name);
};
return o;
}
var person1 = createPerson(“Nicholas”, 29, “Software Engineer”);
var person2 = createPerson(“Greg”, 27, “Doctor”);
缺點(diǎn):工廠模式雖然解決了創(chuàng)建多個(gè)相識(shí)對(duì)象的問題,但卻沒有解決對(duì)象識(shí)別的問題(即怎樣知道一個(gè)對(duì)象的類型)。
?。╞)構(gòu)造函數(shù)模式:ECMAScript中的構(gòu)造函數(shù)可以用來創(chuàng)建特定類型的對(duì)象??梢詣?chuàng)建自定義的構(gòu)造函數(shù),從而定義自定義對(duì)象類型的屬性和方法。
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert(this.name);
};
}
var person1 = new Person(“Nicholas”, 29, “Software Engineer”);
var person2 = new Person(“Greg”, 27, “Doctor”);
缺點(diǎn):使用構(gòu)造函數(shù)的主要問題,就是每個(gè)方法都要在每個(gè)實(shí)例上重新創(chuàng)建一遍。不要忘了——ECMAScript中的函數(shù)是對(duì)象,因此每定義一個(gè)函數(shù),
就是實(shí)例化一個(gè)對(duì)象。
?。╟)原型模式:我們創(chuàng)建的每個(gè)函數(shù)都有一個(gè)prototype(原型)屬性,這個(gè)屬性是一個(gè)指針,指向一個(gè)對(duì)象,而這個(gè)對(duì)象的用途是包含可以由特定類型
的所有實(shí)例共享的屬性和方法。使用原型對(duì)象的好處是可以讓所有對(duì)象共享它包含的屬性和方法
function Person(){
}
Person.prototype.name = “Nicholas”;
Person.prototype.age = 29;
Person.prototype.job = “Software Engineer”;
Person.prototype.sayName = function(){
alert(this.name);
};
var person1 = new Person();
person1.sayName(); //”Nicholas”
var person2 = new Person();
person2.sayName(); //”Nicholas”
alert(person1.sayName == person2.sayName); //true
缺點(diǎn):原型中所有屬性是被很多實(shí)例共享的,這種共享對(duì)于函數(shù)非常合適。但是對(duì)于引用類型值的屬性來說,問題就比較突出了。
?。╠)組合使用構(gòu)造函數(shù)模式和原型模式:創(chuàng)建自定義類型的最常見方式,就是使用組合使用構(gòu)造函數(shù)模式和原型模式。構(gòu)造函數(shù)模式用于定義實(shí)例屬性,
而原型模式用于定義方法和共享的屬性。
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.friends = [“Shelby”, “Court”];
}
Person.prototype = {
constructor: Person,
sayName : function () {
alert(this.name);
}
};
var person1 = new Person(“Nicholas”, 29, “Software Engineer”);
var person2 = new Person(“Greg”, 27, “Doctor”);
person1.friends.push(“Van”);
alert(person1.friends); //”Shelby,Court,Van”
alert(person2.friends); //”Shelby,Court”
alert(person1.friends === person2.friends); //false
alert(person1.sayName === person2.sayName); //true
答案:
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<style type="text/css" rel="stylesheet">
</style>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
window.onload=function()
{
var Man;
//+++++++++++答題區(qū)域+++++++++++
Man=function(obj){
if(!(this instanceof Man))
{
return new Man(obj);
}
this.attrObj=obj||{};
this.wordsObj=[];
}
Man.prototype={
constructor:Man,
words:function(word){
word!=undefined&&this.wordsObj.push(word);
},
attr:function(attribute,attributeValue)
{
var defaultVaule="<用戶未輸入>";
if(arguments.length==2){
this.attrObj[attribute]=attributeValue;
}
else if(!(attribute instanceof Object))
{
if((this.attrObj[attribute]===undefined))
{
return defaultVaule;
}
else
{
return this.attrObj[attribute];
}
}
else{
for(property in attribute)
{
this.attrObj[property]=attribute[property];
}
}
},
say:function()
{
var limit=this.attrObj['words-limit'],
outputString,
wordsLen=this.wordsObj.length;
outputString=this.attr("fullname")+this.attr("words-emote")+":";
for(var i=0;i<limit;i++)
{
outputString+=this.wordsObj[i];
}
return outputString;
}
};
//+++++++++++答題結(jié)束+++++++++++
try{
var me = Man({ fullname: "小紅" });
var she = new Man({ fullname: "小紅" });
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小紅
我的性別是:<用戶未輸入>
------------------*/
me.attr("fullname", "小明");
me.attr("gender", "男");
me.fullname = "廢柴";
me.gender = "人妖";
she.attr("gender", "女");
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小明
我的性別是:男
------------------*/
console.group();
console.info("我的名字是:" + she.attr("fullname") + "\n我的性別是:" + she.attr("gender"));
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
我的名字是:小紅
我的性別是:女
------------------*/
me.attr({
"words-limit": 3,
"words-emote": "微笑"
});
me.words("我喜歡看視頻。");
me.words("我們的辦公室太漂亮了。");
me.words("視頻里美女真多!");
me.words("我平時(shí)都看優(yōu)酷!");
console.group();
console.log(me.say());
/*------[執(zhí)行結(jié)果]------
小明微笑:"我喜歡看視頻。我們的辦公室太漂亮了。視頻里美女真多!"
------------------*/
me.attr({
"words-limit": 2,
"words-emote": "喊"
});
console.log(me.say());
console.groupEnd();
/*------[執(zhí)行結(jié)果]------
小明喊:"我喜歡看視頻。我們的辦公室太漂亮了。"
------------------*/
}catch(e){
console.error("執(zhí)行出錯(cuò),錯(cuò)誤信息: " + e);
}
}
</script>
</html>
您可能感興趣的文章:
- 面向?qū)ο蟮腏avascript之二(接口實(shí)現(xiàn)介紹)
- Javascript 面向?qū)ο螅ㄈ┙涌诖a
- JavaScript面向?qū)ο笾薪涌趯?shí)現(xiàn)方法詳解
- Javascript之面向?qū)ο?-接口
- JavaScript接口的實(shí)現(xiàn)三種方式(推薦)
- JavaScript接口實(shí)現(xiàn)代碼 (Interfaces In JavaScript)
- Javascript 面向?qū)ο螅ㄒ唬?共有方法,私有方法,特權(quán)方法)
- 淺談Javascript面向?qū)ο缶幊?/a>
- js面向?qū)ο笾R妱?chuàng)建對(duì)象的幾種方式(工廠模式、構(gòu)造函數(shù)模式、原型模式)
- javascript面向?qū)ο笕腴T基礎(chǔ)詳細(xì)介紹
- Javascript 面向?qū)ο螅ǘ┓庋b代碼
- JavaScript 接口原理與用法實(shí)例詳解
相關(guān)文章
JavaScript 對(duì)象的屬性和方法4種不同的類型
JavaScript里,對(duì)象的屬性和方法支持4種不同的類型,需要的朋友可以參考下。2010-03-03
javascript面向?qū)ο蟮姆绞綄?shí)現(xiàn)的彈出層效果代碼
由于本人以前是.net程序員,所以即使現(xiàn)在在做前端,也習(xí)慣于用面向?qū)ο蟮姆绞骄帉慾s腳本,我想如果你以前也是或者現(xiàn)在還是名第三代程序員的話,應(yīng)該對(duì)此并不陌生。2010-01-01
javascript 面向?qū)ο蟮腏avaScript類
這一節(jié)來說下緊接著的一個(gè)概念——類。雖然JavaScript中沒有class關(guān)鍵字,但作為開發(fā)人員我們一定要有這個(gè)思想。在C#中類可以分為實(shí)例類和靜態(tài)類,JavaScript亦然。2010-05-05
JavaScript 設(shè)計(jì)模式學(xué)習(xí) Singleton
JavaScript設(shè)計(jì)模式學(xué)習(xí) Singleton2009-07-07
AppBaseJs 類庫 網(wǎng)上常用的javascript函數(shù)及其他js類庫寫的
AppBaseJs類庫。一個(gè)借鑒了網(wǎng)上常用的函數(shù)及其他js類庫寫的,方便大家的調(diào)用。2010-03-03
js面向?qū)ο?多種創(chuàng)建對(duì)象方法小結(jié)
js面向?qū)ο?多種創(chuàng)建對(duì)象方法小結(jié),需要的朋友可以參考下2012-05-05
javascript 面向?qū)ο缶幊?萬物皆對(duì)象
javascript幾乎成了如今web開發(fā)人員必學(xué)必會(huì)的一門語言,但很多人卻只停在了一些表單驗(yàn)證等基礎(chǔ)操作層面上,在面向?qū)ο笳Z言大行其道的當(dāng)下,我們需要去學(xué)習(xí)javascript的面向?qū)ο蟮闹R(shí),以便更好的掌握javascript、為深入理解各種腳本框架打好基礎(chǔ)。2009-09-09

