/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Where did Array()’s properties and methods go?

Okay I have an interesting question. (Or I think it’s interesting).

the codelet

[code]for(var something in Object) alert(something)[/code]

will pop up an alert box for each of “Object”‘s properties and methods telling you it’s name.

You can test this with any inherent javascript object (i don’t exactly recommend you try these as you will get a looot of pop-ups):

[code]for(var something in window) alert(something);
for(var something in document) alert(something);
for(var something in new XMLHttpRequest()) alert(something);[/code]

Here’s the anomaly: the Array object.

[code]for(var something in new Array()) alert(something)[/code]

returns nothing. Nothing at all. But we all know the Array object has methods and properties, like “.length” and “.push()” and “.pop()” and “.substr()”. Where did they all go?

I understand why they have done this, it’s to provide support for associative arrays:

[code]var associativeArray = new Array();
associativeArray[‘var1’] = ‘hello’;
associativeArray[‘var2’] = ‘world’;
for(var something in associativeArray) alert(something);[/code]

So this will just find the two defined variables, “var1” and “var2” rather than finding “length”,”pop”,”push” etc. as well.

But I don’t understand how? How did they do this? How do you hide all the properties and methods in an object, as it were?

If anyone could shed any light on this it would be appreciated.

P.s. I don’t approve of the

[code]associativeArray = new Array()[/code]

method. All the associative array is is an empty object that you can add properties to. You should write it

[code]associativeArray = new Object()[/code]

and then you wouldn’t need this weird behaviour of the Array object.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@felgallMay 02.2007 — Not all methods and properties have the isEnumerable flag set. Those that don't are only able to be accesed directly and not using for/in.
Copy linkTweet thisAlerts:
@theRatWonderauthorMay 02.2007 — oooh. It's isEnumerable. So it's nothing special.

Thanks.
Copy linkTweet thisAlerts:
@mrhooMay 02.2007 — You should have examined the Array.[B]prototype [/B]object,not the Array constructor, but that's OK-you would have got the same result.

Try the same thing for String, Number, RegExp, Date and so on.

Some built in properties are tagged not-enumerable.

There is a special test for it-

Array.prototype.propertyIsEnumerable('length') returns false, not undefined.

I believe it is so young coders won't hurt themselves.

Note that All user defined properties and methods are supposed to be enumerable, and most are, but when you define a toString method for an object, [B]IE [/B]won't enumerate it, though it will return it if you specifically ask for it.

To get the toString method of an object called Group returned as a string of source code:

Function.prototype.toString.call(Group.prototype.toString)

As far as I know, only IE has this behavior, and I have only seen it when debugging. It knows the method, it just thinks it is the original inherited toString property, apparently.

Firefox and Opera both enumerate toString methods of custom objects, but you have to drag it out of IE.
Copy linkTweet thisAlerts:
@theRatWonderauthorMay 02.2007 — A wealth of knowledge.

My internal JavaScript knowledge bank is feeling nourished.

Thanks guys.
×

Success!

Help @theRatWonder spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...