最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

從面試題學(xué)習(xí)Javascript 面向?qū)ο螅▌?chuàng)建對(duì)象)

 更新時(shí)間:2012年03月30日 00:37:33   作者:  
從面試題學(xué)習(xí)Javascript 面向?qū)ο螅▌?chuàng)建對(duì)象),學(xué)習(xí)js的朋友可以參考下
題目:
復(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ì)象定義為:“無(wú)序?qū)傩缘募希鋵傩钥梢园局?、?duì)象或者函數(shù)”。
(2)JS創(chuàng)建對(duì)象的方法:
 ?。╝)工廠模式:用函數(shù)來(lái)封裝以特定接口創(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ì)象的問(wèn)題,但卻沒(méi)有解決對(duì)象識(shí)別的問(wèn)題(即怎樣知道一個(gè)對(duì)象的類型)。
 ?。╞)構(gòu)造函數(shù)模式:ECMAScript中的構(gòu)造函數(shù)可以用來(lái)創(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ù)的主要問(wèn)題,就是每個(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ì)于引用類型值的屬性來(lái)說(shuō),問(wèn)題就比較突出了。
   ?。╠)組合使用構(gòu)造函數(shù)模式和原型模式:創(chuàng)建自定義類型的最常見(jiàn)方式,就是使用組合使用構(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>

相關(guān)文章

最新評(píng)論

苍溪县| 屯门区| 八宿县| 东海县| 宁阳县| 湘潭县| 岳阳县| 金阳县| 贵德县| 石阡县| 永川市| 潍坊市| 阿荣旗| 台南县| 荔波县| 无棣县| 棋牌| 通渭县| 珠海市| 义乌市| 泰安市| 永济市| 伊宁市| 龙海市| 图片| 石首市| 家居| 固始县| 土默特左旗| 城步| 唐河县| 新闻| 化州市| 德令哈市| 枣庄市| 杭锦旗| 章丘市| 怀安县| 张家港市| 汕头市| 民乐县|