Prototypes and enumerable properties

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I'd like to add a method foo() to all objects:

Object.prototype.foo=function() {
// whatever
}

The caveat, though, is that I do not want foo to be enumerable by a
for-in loop. Can I do that? If not, is there a way to derive a
subclass of Object for which foo is not enumerable?
 
D

Douglas Crockford

I'd like to add a method foo() to all objects:
Object.prototype.foo=function() {
// whatever
}

The caveat, though, is that I do not want foo to be enumerable by a
for-in loop. Can I do that? If not, is there a way to derive a
subclass of Object for which foo is not enumerable?

No. I think this was a design error in the language itself. The
JavaScript VM has a DONTENUM property, but it not available to the
programmer.

The best you can do is exclude functions while enumerating.

for (key in object) {
if (typeof object[key] != 'function') {
do your thing
}
}

http://www.crockford.com/javascript
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top