從面試題學(xué)習(xí)Javascript 面向?qū)ο螅▌?chuàng)建對象)
更新時間:2012年03月30日 00:37:33 作者:
從面試題學(xué)習(xí)Javascript 面向?qū)ο螅▌?chuàng)建對象),學(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("我平時都看優(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í)行出錯,錯誤信息: " + e);
}
知識點(diǎn):
(1)JS面向?qū)ο蠡A(chǔ):ECMA-262把對象定義為:“無序?qū)傩缘募?,其屬性可以包含基本值、對象或者函?shù)”。
(2)JS創(chuàng)建對象的方法:
?。╝)工廠模式:用函數(shù)來封裝以特定接口創(chuàng)建對象的細(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òu)造函數(shù)模式:ECMAScript中的構(gòu)造函數(shù)可以用來創(chuàng)建特定類型的對象??梢詣?chuàng)建自定義的構(gòu)造函數(shù),從而定義自定義對象類型的屬性和方法。
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ù)的主要問題,就是每個方法都要在每個實(shí)例上重新創(chuàng)建一遍。不要忘了——ECMAScript中的函數(shù)是對象,因此每定義一個函數(shù),
就是實(shí)例化一個對象。
(c)原型模式:我們創(chuàng)建的每個函數(shù)都有一個prototype(原型)屬性,這個屬性是一個指針,指向一個對象,而這個對象的用途是包含可以由特定類型
的所有實(shí)例共享的屬性和方法。使用原型對象的好處是可以讓所有對象共享它包含的屬性和方法
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í)例共享的,這種共享對于函數(shù)非常合適。但是對于引用類型值的屬性來說,問題就比較突出了。
?。╠)組合使用構(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("我平時都看優(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í)行出錯,錯誤信息: " + 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("我平時都看優(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í)行出錯,錯誤信息: " + e);
}
知識點(diǎn):
(1)JS面向?qū)ο蠡A(chǔ):ECMA-262把對象定義為:“無序?qū)傩缘募?,其屬性可以包含基本值、對象或者函?shù)”。
(2)JS創(chuàng)建對象的方法:
?。╝)工廠模式:用函數(shù)來封裝以特定接口創(chuàng)建對象的細(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òu)造函數(shù)模式:ECMAScript中的構(gòu)造函數(shù)可以用來創(chuàng)建特定類型的對象??梢詣?chuàng)建自定義的構(gòu)造函數(shù),從而定義自定義對象類型的屬性和方法。
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ù)的主要問題,就是每個方法都要在每個實(shí)例上重新創(chuàng)建一遍。不要忘了——ECMAScript中的函數(shù)是對象,因此每定義一個函數(shù),
就是實(shí)例化一個對象。
(c)原型模式:我們創(chuàng)建的每個函數(shù)都有一個prototype(原型)屬性,這個屬性是一個指針,指向一個對象,而這個對象的用途是包含可以由特定類型
的所有實(shí)例共享的屬性和方法。使用原型對象的好處是可以讓所有對象共享它包含的屬性和方法
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í)例共享的,這種共享對于函數(shù)非常合適。但是對于引用類型值的屬性來說,問題就比較突出了。
?。╠)組合使用構(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("我平時都看優(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í)行出錯,錯誤信息: " + 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)建對象的幾種方式(工廠模式、構(gòu)造函數(shù)模式、原型模式)
- javascript面向?qū)ο笕腴T基礎(chǔ)詳細(xì)介紹
- Javascript 面向?qū)ο螅ǘ┓庋b代碼
- JavaScript 接口原理與用法實(shí)例詳解
相關(guān)文章
javascript面向?qū)ο蟮姆绞綄?shí)現(xiàn)的彈出層效果代碼
由于本人以前是.net程序員,所以即使現(xiàn)在在做前端,也習(xí)慣于用面向?qū)ο蟮姆绞骄帉慾s腳本,我想如果你以前也是或者現(xiàn)在還是名第三代程序員的話,應(yīng)該對此并不陌生。2010-01-01
javascript 面向?qū)ο蟮腏avaScript類
這一節(jié)來說下緊接著的一個概念——類。雖然JavaScript中沒有class關(guān)鍵字,但作為開發(fā)人員我們一定要有這個思想。在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類庫。一個借鑒了網(wǎng)上常用的函數(shù)及其他js類庫寫的,方便大家的調(diào)用。2010-03-03
js面向?qū)ο?多種創(chuàng)建對象方法小結(jié)
js面向?qū)ο?多種創(chuàng)建對象方法小結(jié),需要的朋友可以參考下2012-05-05

