DontEnum with custom property

X

xdevel1999

Hi,

when I do something like:

Object.prototype.info = function(){};

var o = {propr:1};

for(var i in o)
...

here i contains also info inherited from its prototype

Now If I wanted to enumerate only the object property how can I do
that?


P.S.
It seems we cannot set DontEnum bacause it is a property reserved by
the language itself

Any idea?

May be in real life we should not extends the language itself... but
so we don't use the prototype power!!!
 
J

Jeremy J Starcher

Hi,

when I do something like:

Object.prototype.info = function(){};

var o = {propr:1};

for(var i in o)
...

here i contains also info inherited from its prototype

Now If I wanted to enumerate only the object property how can I do that?


P.S.
It seems we cannot set DontEnum bacause it is a property reserved by the
language itself

Any idea?

May be in real life we should not extends the language itself... but so
we don't use the prototype power!!!

There are common solutions:

1) Don't augment the object prototype.
2) Use another form like:

for(var i in o)
if (o.hasOwnProperty(i)) {
// Do it.
}
}

Unfortunately, one must feature test for hasOwnProperty, as it is a later
addition to the language.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top