搜狐JvaaScript面试题:实现一个叫Man的类,包含attr, words, say三个方法。

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、代码注释明确

largeQ
浏览 556回答 1
1回答

MMTTMM

//+++++++++++答题区域+++++++++++//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: "小红" });}//+++++++++++答题结束+++++++++++
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript