亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

搜狐JvaaScript面試題:實現一個叫Man的類,包含attr, words, say三個方法。

搜狐JvaaScript面試題:實現一個叫Man的類,包含attr, words, say三個方法。

largeQ 2018-12-07 08:36:04
var Man;//+++++++++++答題區域+++++++++++//+++++++++++答題結束+++++++++++try{ var me = Man({ fullname: "小紅" }); var she = new Man({ fullname: "小紅" }); console.group(); console.info("我的名字是:" + me.attr("fullname") + "\n我的性別是:" + me.attr("gender")); console.groupEnd(); /*------[執行結果]------ 我的名字是:小紅 我的性別是:<用戶未輸入> ------------------*/ 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(); /*------[執行結果]------ 我的名字是:小明 我的性別是:男 ------------------*/ console.group(); console.info("我的名字是:" + she.attr("fullname") + "\n我的性別是:" + she.attr("gender")); console.groupEnd(); /*------[執行結果]------ 我的名字是:小紅 我的性別是:女 ------------------*/ me.attr({ "words-limit": 3, "words-emote": "微笑" }); me.words("我喜歡看視頻。"); me.words("我們的辦公室太漂亮了。"); me.words("視頻里美女真多!"); me.words("我平時都看優酷!"); console.group(); console.log(me.say()); /*------[執行結果]------ 小明微笑:"我喜歡看視頻。我們的辦公室太漂亮了。視頻里美女真多!" ------------------*/ me.attr({ "words-limit": 2, "words-emote": "喊" }); console.log(me.say()); console.groupEnd(); /*------[執行結果]------ 小明喊:"我喜歡看視頻。我們的辦公室太漂亮了。" ------------------*/ }catch(e){ console.error("執行出錯,錯誤信息: " + e);} 要求:1、只能在指定的位置填寫自己的代碼,本文件里的其他代碼不能修改2、所有題目都不允許添加全局變量名3、本文件應該能在firebug的console里正常執行,并輸出結果4、代碼最優化,效率最高5、代碼注釋明確
查看完整描述

1 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

//+++++++++++答題區域+++++++++++
//
Animal封裝類
function Animal(__obj){
this.obj=__obj;//接收參數
this.fullname=this.obj.fullname;//取到fullname的值
this.gender = "<用戶未輸入>"; //定義一個gender變量,并初始化值為<用戶未輸入>
this.full="";//存儲fullname的值,以防從外面改變fullname的值
this.gen="";//存儲gender的值,以防從外面改變gender的值
this.wordsArr=[];//定義一個數組,存儲words字符串
this.index=0;//定義wordsArr數組索引值
this.count=0;//定義count用來接收words-limit的值
}
Animal.prototype.attr=function(){
if(typeof(arguments[0])=="object"){//判斷參數類型
this.sayStr="小明";//定義個一個變量,并初始化值,用來拼接字符串
this.count=arguments[0]["words-limit"];//取到對象傳入的值
this.sayStr+=arguments[0]["words-emote"];//取到對象傳入的值
}else if(typeof(arguments[0])=="string"){//判斷參數類型
if(arguments[0]=="fullname"){//判斷參數
if(arguments[1]){
this.full=arguments[1];//取到fullname的值,并存入this.full變量里面
}else if(this.full==""){
this.full=this.fullname;//當首次實例化的時候,fullname的值沒有傳入
}
return this.full;
}else if(arguments[0]=="gender"){//判斷參數
if(arguments[1]){
this.gen=arguments[1];//取到gender的值,并存入this.gen變量里面
}else if(this.gen==""){
this.gen=this.gender;//當首次實例化的時候,gender的值沒有傳入
}
return this.gen;
}
}else{
console.log("參數不是約定類型,請傳入object或者string");//如果參數類型不是約定的類型,提示
}
}
Animal.prototype.words=function(){
this.wordsArr[this.index]=arguments[0];//取到words的值并存入數組中
this.index++;//索引值
}
Animal.prototype.say=function(){
this.wordsStr="";//定義一個變量,把數組轉換成字符串
for(var i=0;i<this.count;i++){
this.wordsStr+=this.wordsArr[i];//類型轉換
}
return this.sayStr+':"'+this.wordsStr+'"';//最后拼接字符串,并返回
}
Man=function(__obj){
return new Animal(__obj);//這里引用了閉包,用來支持 var me = Man({ fullname: "小紅" });var she = new Man({ fullname: "小紅" });
}
//+++++++++++答題結束+++++++++++
查看完整回答
反對 回復 2018-12-24
  • 1 回答
  • 0 關注
  • 563 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號