Object.create

function a(){}
a.prototype.age=1
new a().age//1
var p=Object.create(new a)
p.age//1
a.prototype.age=4
p.age//4

Object.create(a.prototype).age//4
Object.create(a.prototype).__proto__ === a.prototype//true
Object.create(new a).__proto__ === a.prototype//false
Object.create(new a().__proto__).__proto__ === a.prototype//true
new a().__proto__ === a.prototype//true

创建一个具有指定原型且可选择性地包含指定属性的对象

    var fun = function(){}

    Object.create(fun.prototype,descriptors).__proto__ === fun.prototype

  • prototype:必需。 要用作原型的对象。 可以为 null

  • descriptors:可选。 包含一个或多个属性描述符的 JavaScript 对象

  • 返回值:一个具有指定的内部原型且包含指定的属性(如果有)的新对象。

  • 若要停止原型链,可以使用采用了 null prototype 参数的函数。 所创建的对象将没有原型


Object.create(null) 创建的对象是一个空对象,在该对象上没有继承 Object.prototype 原型链上的属性或者方法,例如:toString(), hasOwnProperty()等方法

var a = {}
a.toString()
//"[object Object]"

var b = Object.create(null)
b.toString()
//VM273:1 Uncaught TypeError: b.toString is not a function

descriptors

  • 该参数对象不能是 undefined
  • 只有该对象中自身拥有的可枚举的属性才有效,也就是说该对象的原型链上属性无效
var newObj = Object.create(null, {
    size: {
        value: "large",//默认值,默认undefined
        enumerable: true,//默认false
        writable:true,//是否可修改默认false 不能和get/set共存
        configurable:false,//默认false
    },
    shape: {
        enumerable: true,
        get(){
            return 'shape'
        },
        set(){
            console.log('set')
        }
    }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值