JS:理解可写为 Object 属性属性
JS: understanding writeable as Object property attribute
我最近开始玩(和工作)Object.create和类似的ES5东西。我不太了解对象属性的writeable
属性的工作方式。规范说,默认情况下设置为 false,但是如何在不使用 getter 和 setter 的情况下更改它?
或者:为什么像它一样工作这段代码:
编辑:此代码是垃圾!我还不明白 getter 和 setter 在这里是如何工作的!
var ob = Object.create(Object.prototype, {
a: {
value: 'not save'
},
b: {
value: 'value configured, but writeable',
writeable: true
},
c: {
configurable: false,
get: function() {
return c;
},
set: function(string) {
c = string;
}
}
});
ob.c = 'set d the first time'; //not doing this would give us an error in the console.log line (access to ob.c)
console.log(ob.a, ob.b, ob.c); //->not save value configured, but writeable set d the first time
ob.a = 'new a';
ob.b = 'new b';
ob.c = 'new c';
console.log(ob.a, ob.b, ob.c, ob.d); //->not save value configured, but writeable new c
为什么实际上只有 ob.c 是可写的,而不是 ob.b - 或者我做错了什么?
编辑:更好的代码在这里
var ob = Object.create(Object.prototype, {
a: {
value: 'not save',
},
b: {
value: 'value configured, but writeable',
writeable: true
},
c: {
configurable: false,
get: function() {
return this.a; // works, but is not useful because of the naming and a is not writeable
},
set: function(string) {
this.a = string; // works, but is not useful because of the naming and a is not writeable
}
}
});
你有一个错别字。该标准将属性定义为writable
,而不是writeable
。修复b
定义中的拼写,它非常有效:
b: {
value: 'value configured, but writeable',
writable: true
}
相关文章:
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 如何使用object.assign()从其他对象引用基本对象属性
- javascript表单字段submit:Object dos'在IE8中不支持此属性或方法
- Object.create()只读名称属性
- 如何列出使用Object.defineProperty定义的属性
- 未捕获的类型错误:属性'$'对象[object Window]的
- 用于更改object.object属性的函数
- 在object.defineProperty之前在对象初始化中设置属性
- 正在覆盖Object.obsoe()中的属性
- 直接访问Parse.Object's使用点表示法的属性
- Ember.js with EmberFire Object - 如何使用数组属性
- 使用 Object.create() 将函数与原型上的属性合并
- Javascript object.defineProperties 以自动更新属性
- Object 在 ie8 for javascript 中不支持此属性或方法
- Google Maps JavaScript : 未捕获错误: 属性
- Ember控制器计算的属性(Object类型)更新不起作用
- 谷歌地图绘图API:属性
- 类属性(Object.assign)的Setter/getter
- jquery在Asp.net MVC中的验证错误-无法获取属性': object为空或未定义
- D3:属性[object object]无效