码迷,mamicode.com
首页 > 其他好文 > 详细

Object.beget(stooge);

时间:2014-09-30 15:06:09      阅读:1502      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   sp   c   on   cti   r   new   

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

标签:io   os   ar   sp   c   on   cti   r   new   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!