admin管理员组文章数量:1221320
I'm trying to define a non-enumerable toJSON
function on a prototype object without much luck. I'm hoping for something similar to ECMAScript 5 toJSON
:
Object.defineProperty(obj, prop, { enumerable: false });
However this defines it as a property which cannot be accessed as a method.
[EDIT: Nick is wrong; it can be accessed as a method. His mistake was in code that is not shown in this question - see his comments on answers below, for details.]
I was hoping to be able to define the function in a non-enumerable fashion, as I was planning to define in the prototypes of all primitive types (String
, Number
, Boolean
, Array
, and Object
), so that I can recursively apply the function through complex objects.
The end goal here is to be able JSONify a Backbone model/collection with nested collections recursively.
I guess in total I have two main questions:
- Is it possible to define a non-enumerable function on a prototype? If so how?
- Is there a better way to JSONify nested Backbone models?
I'm trying to define a non-enumerable toJSON
function on a prototype object without much luck. I'm hoping for something similar to ECMAScript 5 toJSON
:
Object.defineProperty(obj, prop, { enumerable: false });
However this defines it as a property which cannot be accessed as a method.
[EDIT: Nick is wrong; it can be accessed as a method. His mistake was in code that is not shown in this question - see his comments on answers below, for details.]
I was hoping to be able to define the function in a non-enumerable fashion, as I was planning to define in the prototypes of all primitive types (String
, Number
, Boolean
, Array
, and Object
), so that I can recursively apply the function through complex objects.
The end goal here is to be able JSONify a Backbone model/collection with nested collections recursively.
I guess in total I have two main questions:
- Is it possible to define a non-enumerable function on a prototype? If so how?
- Is there a better way to JSONify nested Backbone models?
3 Answers
Reset to default 16I don't get it, why can't you access it as a method?
var foo = {};
Object.defineProperty(foo, 'bar', {
enumerable: false,
value: function () {console.log('foo.bar\'d!');}
});
foo.bar(); // foo.bar'd!
If you wanted it on the prototype, it's as easy as
Object.defineProperty(foo.prototype, /* etc */);
or even directly in Object.create
foo.prototype = Object.create(null, {
'bar': {value: function () {/* ... */}}
});
However, unless you're creating instances of foo
, it won't show up if you try to foo.bar
, and only be visible as foo.prototype.bar
.
If foo
has it's own prototype (e.g. foo = Object.create({})
), you can get it with Object.getPrototypeOf
, add the property to that and then foo.bar
would work even if it is not an instance.
var proto = Object.getPrototypeOf(foo); // get prototype
Object.defineProperty(proto, /* etc */);
You can see visibility of enumerable vs non-enumerable properties here.
Paul S. is right about needing to set the property definition's value
instead of a get
, but I wanted to add that you don't need to pass enumerable: false
, because false is the default for that option in Object.defineProperty()
The answer can be simplified to:
var foo = {};
Object.defineProperty(foo, 'bar', {
value: function(){ console.log('calling bar!'); }
});
foo.bar();
Always you can avoid enumerable functions properties in object when you looping through it. And instead of define property in each object and set enumerable to false , you can create function which will call to any object with the property you want and put a condition to not take the property in the looping list. here is the example :
const obj = {
name: "myName",
title: "developer"
}
function prop() {
this.loop = function(i) {
for (i in this) {
if (typeof(this[i]) == "function") {
continue;
} else {
console.log(this[i]);
}
}
}
}
prop.call(obj);
obj.loop();
output >> myName, developer
本文标签: javascriptJS nonenumerable functionStack Overflow
版权声明:本文标题:javascript - JS non-enumerable function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739359223a2159738.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
for (x in obj)
– Nick Mitchinson Commented Jul 3, 2013 at 2:15