admin管理员组文章数量:1401208
So I know you can do this:
var obj = {};
Object.defineProperty(obj, 'staticProp', {
value: 'I will never change',
writable: 'false'
});
And I know you can do this:
var obj = {
get gettableProp(){
return 'gettable!'
}
}
Is there a way to define non-writable/enumerable/configurable properties declaratively instead of using Object.defineProperty(), the way you'd define a getter or setter?
The reason I ask is because I have a function that gets passed an object like this:
ObjectProcessor({
// A bunch of properties
})
I'd really like to be able to keep that simple syntax for cases when I'd want to include non-writable or non-enumerable properties, rather than having to do
var obj = {}
Object.defineProperty(obj, 'staticProp', {
value: 'I will never change',
writable: 'false'
});
ObjectProcessor(obj);
So I know you can do this:
var obj = {};
Object.defineProperty(obj, 'staticProp', {
value: 'I will never change',
writable: 'false'
});
And I know you can do this:
var obj = {
get gettableProp(){
return 'gettable!'
}
}
Is there a way to define non-writable/enumerable/configurable properties declaratively instead of using Object.defineProperty(), the way you'd define a getter or setter?
The reason I ask is because I have a function that gets passed an object like this:
ObjectProcessor({
// A bunch of properties
})
I'd really like to be able to keep that simple syntax for cases when I'd want to include non-writable or non-enumerable properties, rather than having to do
var obj = {}
Object.defineProperty(obj, 'staticProp', {
value: 'I will never change',
writable: 'false'
});
ObjectProcessor(obj);
Share
Improve this question
asked Jun 25, 2015 at 21:44
ZacqaryZacqary
2152 silver badges8 bronze badges
3 Answers
Reset to default 5Is there a way to define non-writable/enumerable/configurable properties declaratively instead of using Object.defineProperty(), the way you'd define a getter or setter?
No.
(there is Object.defineProperties
, but I guess that's not what you are looking for)
No, there is no other method (except for Object.defineProperties
and Object.create
that allow you to provide multiple of these descriptors).
I'd really like to be able to keep that simple syntax
Notice that Object.defineProperty
returns the passed object, so you can simplify your code to
ObjectProcessor(Object.defineProperty({}, 'staticProp', {
value: 'I will never change',
writable: 'false'
}));
and avoid introducing that extra obj
variable.
With a naming convention, you can build it into your ObjectProcessor:
var prefix = 'readonly_';
function ObjectProcessor(obj) {
Object.keys(obj).filter(function (name) {
// find property names which match your convention
return name.indexOf(prefix) === 0;
}).forEach(function (name) {
// make the new property readonly
Object.defineProperty(obj, name.substring(prefix.length), {
value: obj[name],
writable: false
});
// remove the old, convention-named property
delete obj[name];
});
// remaining ObjectProcessor logic...
}
This lets you handle writable and readonly properties:
ObjectProcessor({
readonly_id: 1,
name: 'Foo'
});
本文标签: javascriptDefining nonwritable properties without ObjectdefinePropertyStack Overflow
版权声明:本文标题:javascript - Defining non-writable properties without Object.defineProperty? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744254385a2597406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论