首页 > 其他 > 详细

Object.beget(stooge);

时间:2014-09-30 12:51:39      阅读:928      评论:0      收藏:0      [点我收藏+]

<script>
var stooge = {
    "first-name": "Jerome",
    "last-name": "Howard"
};
stooge[‘middle-name‘] = ‘Lester‘;
stooge.nickname = ‘Curly‘;
//以上填充4个元素

if (typeof Object.beget !== ‘function‘) {
     Object.beget = function (o) {
         var F = function () {};
         F.prototype = o;
         return new F();
     };
}
var another_stooge = Object.beget(stooge);


another_stooge[‘first-name‘] = ‘Harry‘;
another_stooge[‘middle-name‘] = ‘Moses‘;
another_stooge.nickname = ‘Moe‘;
//以上填充3个元素

stooge.profession = ‘actor‘;
//原型填充1个元素:因为本身another_stooge没有这个属性,所以到原型去寻找
another_stooge.profession    // ‘actor‘<br />
//如果本身有这个属性,就取这个属性
another_stooge.nickname    // ‘Moe‘

delete another_stooge.nickname;

another_stooge.nickname;    // ‘Curly‘
</script>

Object.beget(stooge);

原文:http://www.cnblogs.com/dingyuanxin/p/4001688.html

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