Why can't I redefine a property in a Javascript object?

前端 未结 4 1496
日久生厌
日久生厌 2020-12-08 13:50

I am creating a object using Object.create and I want to add properties to it.

> var o = Object.create({});
undefined

> Object.defineProp         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 14:06

    You are unable to redefine the property because Object.defineProperty() defaults to non-configurable properties, from the docs:

    configurable

    true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object. Defaults to false.

    So this defaults to false - you'd need to pass it configurable: true to allow it.

提交回复
热议问题