实现一个叫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.先来一个构造函数

Man=function(info){

};

2.

var me = Man({ fullname: "小红" });
var she = new Man({ fullname: "小红" });

 

 

 

 

 

 

 

 

 

 

 

为什么要实例化对象

实例化对象就是申请堆栈空间的过程,实例化的对象拥有内存空间后才能进行一系列的操作

简单来说:你定义了一个人的类,人有那么多,你怎么知道是哪个人,实例化后,就定位到某个人上面去了

posted on 2014-09-26 14:10  鬼畜十三  阅读(199)  评论(0编辑  收藏  举报