object create_Object create()方法

object create

Introduced in ES5.

在ES5中引入。

Creates a new object, with the specified prototype.

使用指定的原型创建一个新对象。

Usage:

用法:

const newObject = Object.create(prototype)

Example:

例:

const animal = {}
const dog = Object.create(animal)

The newly create object will inherit all the prototyope object properties.

新创建的对象将继承所有原型对象的属性。

You can specify a second parameter to add new properties to the object, that the prototype lacked:

您可以指定第二个参数以向原型添加缺少的新属性:

const newObject = Object.create(prototype, newProperties)

where newProperties is an object of objects that define each property.

其中newProperties是定义每个属性的对象的对象。

Example:

例:

const animal = {}
const dog = Object.create(animal, {
  breed: {
    value: 'Siberian Husky'
  }
});
console.log(dog.breed) //'Siberian Husky'

I didn’t just say breed: 'Siberian Husky' but I had to pass a property descriptor object, defined at the beginning of this page.

我不只是说breed: 'Siberian Husky'而是我必须传递在页面开头定义的属性描述符对象。

Object.create() is often used in combination with Object.assign():

Object.create()通常与Object.assign()结合使用:

const dog = Object.assign(Object.create(animal), {
  bark() {
    console.log('bark')
  }
})

翻译自: https://flaviocopes.com/javascript-object-create/

object create

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值