首页 > 其他 > 详细

不使用类的继承,纯粹使用对象

时间:2016-01-26 18:08:07      阅读:142      评论:0      收藏:0      [点我收藏+]
 var myMammal = {
    name : ‘Herb the Mammal‘,
    get_name : function () {
        return this.name;
    },
    says : function () {
        return this.saying || ‘‘;
    }
};

var myCat = Object.create(myMammal);
myCat.name = ‘Henrietta‘;
myCat.saying = ‘meow‘;
myCat.purr = function (n) {
    var i, s = ‘‘;
    for (i = 0; i < n; i += 1) {
        if (s) {
            s += ‘-‘;
        }
        s += ‘r‘;
    }
    return s;
};
myCat.get_name = function () {
    return this.says() + ‘ ‘ + this.name + ‘ ‘ + this.says();
};
alert(myCat.get_name());

不使用类的继承,纯粹使用对象

原文:http://www.cnblogs.com/chuangweili/p/5161031.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!