Object.beget(stooge);

<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>

posted @ 2014-09-30 12:04  丁元新  阅读(1635)  评论(0编辑  收藏  举报
Top