for (var i in o): hidden properties of objects?

O

optimistx

for (var i in obj) {assert( i + ': ' +obj)}

gives some properties of obj, but not all. How to find all the properties of
obj? If one has to know the names before asking their values, how to find
all possible names? The ECMA-262 spec mentions e.g. DontEnum, DontDelete,
Internal, ReadOnly, and there are some browser specific like __proto__ .

I am mainly interested in the properties in the beginning of the prototype
chain, like Object, Object.prototype, Function, {}. Dom -elemets give so
many properties in the for -loop that there is no desire to look fort more,
so far :)
 
G

Gregor Kofler

optimistx meinte:
for (var i in obj) {assert( i + ': ' +obj)}

gives some properties of obj, but not all. How to find all the
properties of obj? If one has to know the names before asking their
values, how to find all possible names? The ECMA-262 spec mentions e.g.
DontEnum, DontDelete, Internal, ReadOnly, and there are some browser
specific like __proto__ .


Some "research" helps:

"A for...in loop does not iterate over built-in properties. These
include all built-in methods of objects, such as String's indexOf method
or Object's toString method. However, the loop will iterate over all
user-defined properties (including any which overwrite built-in
properties)." [1]

Gregor


[1]
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in
 
G

Garrett Smith

Gregor said:
optimistx meinte:
for (var i in obj) {assert( i + ': ' +obj)}

gives some properties of obj, but not all. How to find all the
properties of obj? If one has to know the names before asking their
values, how to find all possible names? The ECMA-262 spec mentions
e.g. DontEnum, DontDelete, Internal, ReadOnly, and there are some
browser specific like __proto__ .


Some "research" helps:

"A for...in loop does not iterate over built-in properties. These
include all built-in methods of objects, such as String's indexOf method
or Object's toString method. However, the loop will iterate over all
user-defined properties (including any which overwrite built-in
properties)." [1]

Gregor


[1]
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in


MDC wiki is user-maintained and it isn't always correct. This is an
example of where MDC is incorrect.

Better reference (though not official):
http://bclary.com/2004/11/07/#a-12.6.4

A |for in| loop iterates over an objects properties using gets the name
of the next property doesn't have the /DontEnum/ attribute.

The /DontEnum/ attribute is an internal flag of the property. Changing
the properties value does not affect this flag.

Object.prototype.toString = function(){
return "42";
};

Clearly that is a user-defined function, not built-in. The /toString/
property got a new /value/ via Simple Assignment but that did not change
the attributes.

Host object properties can also get the /DontEnum/ attribute, for
example, a /CSSRuleList/ have a /length/ property that may or may not
have the /DontEnum/ attribute.


Enumerating the properties of an object includes enumerating properties
of its prototype.

To examine a host object's *own* properties, |hasOwnProperty| may be
used indirectly. Never for production code, you may want to try:-

function getOwnProperties(o) {
var hap = ({}).hasOwnProperty;

var result = [];
for(var p in o) {
if(hap.call(o, p)) {
try {
result.push(p + " = " + o[p]);
} catch(ex) {
result.push(p + ": " + ex.message);
}
}
}
return result;
}

Garrett
 
O

optimistx

Garrett Smith wrote:
....
To examine a host object's *own* properties, |hasOwnProperty| may be
used indirectly. Never for production code, you may want to try:-

function getOwnProperties(o) {
var hap = ({}).hasOwnProperty;

var result = [];
for(var p in o) {
if(hap.call(o, p)) {
try {
result.push(p + " = " + o[p]);
} catch(ex) {
result.push(p + ": " + ex.message);
}
}
}
return result;
}

Garrett
thanks. The list for some objects o can be hundreds of properties, for some
others hardly any. There is probably no way other the the hard way to learn
many of the properties (hard way: searching in the docs, tutorials,
reference books and pages).

Your code is interesting. Is there a special reason to use

if (hap.call(o.p)) ...

instead of

if (o.hasOwnProperty(p))...

?
Ok, learning to do things in new ways , (

oldway.way3 = new Way('tricks');

is fun, of course :)
 
G

Garrett Smith

optimistx said:
Garrett Smith wrote:
...
To examine a host object's *own* properties, |hasOwnProperty| may be
used indirectly. Never for production code, you may want to try:-

function getOwnProperties(o) {
var hap = ({}).hasOwnProperty;

var result = [];
for(var p in o) {
if(hap.call(o, p)) {
try {
result.push(p + " = " + o[p]);
} catch(ex) {
result.push(p + ": " + ex.message);
}
}
}
return result;
}

Garrett
thanks. The list for some objects o can be hundreds of properties, for
some others hardly any. There is probably no way other the the hard way
to learn many of the properties (hard way: searching in the docs,
tutorials, reference books and pages).

Your code is interesting. Is there a special reason to use

if (hap.call(o.p)) ...

instead of

if (o.hasOwnProperty(p))...

?

Host object may not have a hasOwnProperty method. Take IE, for example.

javascript:alert("hasOwnProperty" in document);

However, the host object may be used as |this| for that, and other
native methods, such as Function.prototype.call, but watch out for Array
methods -- they're not compatible with Host object in IE:-

javascript:alert( ([]).slice.call(document.childNodes) );

IE7: Error: Jscript Object Expected.

That error message indicates a the thisArg being something other than a
JScript object caused the error. That is unfortunate, as
slice.call(other) provides a quick method for creating an array out of
another object.

Garrett
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top