Jump to content

Javascript Objects Explained (in depth, please)


kael.shipman

Recommended Posts

Does anyone know the anatomy of a javascript object (like an array or an Option object)? I hate how javascript is so stupid as to reference the highest numeric key in an array and add 1 to construct the array's length property, so I tried to add a count() method to all arrays by writing

 

Array.prototype.count = function() {
var x, a = 0;
for(x in this) a++;
}

//(used like this):
var cha = new Array();
cha['one'] = 1;
cha['two'] = 2;
cha['three'] = 3;
cha['four'] = 4;

alert(cha.count());

 

but when I actually run that function, it alerts 5 instead of four. When I debugged by writing alert(this[x]); instead of a++; in the count function, it showed that the last value in the special variable "this" is actually the function definition. Weird. However, it also showed all of the correct key/value pairs before displaying the function definition. Any thoughts on what the actual object definition of an Array looks like?

 

Also (along those same lines), does anyone know how the for(x in array) construct works? Where does javascript store the properties of an array (correct me if I'm wrong, but an array is simply a subclass of the Object object that extends that parent to include length and some other stuff)? For example, is there an inherent property of the Object class that keeps track of all properties added to it? Perhaps I'm accessing the constructor property in the above example, which is why it's showing me the function definition.

 

Any light on any of this would be cool. Thanks in advance!

Link to comment
Share on other sites

Why are you using a count method  when javascript has a length method for arrays?

 

In javascript do not use associative arrays, it is better to use hashes. If you wanted to have keys of one, two etc do it like ths

 

var array = {

'one': 1,

'two': 2

};

 

also instead of doing new Object or new array, just do

var arr = [];

 

and you use for in loop for objects example

 

var array = {

'one': 1,

'two': 2

};

 

x = 0;

for(y in array){

x++;

}

alert(x);

 

 

 

Link to comment
Share on other sites

First of all, associative arrays are a perfectly valid and useful tool to me. I wanted a count function, though, because the length property of a javascript array is derived not from the actual number of items in the object, but instead from the highest numeric key, so associative arrays are effectively empty to the length property.

 

Second, I'm not looking so much for solutions here as for knowledge. I was more curious about how it actually worked and maybe why it worked that way. For example, I see that you're declaring arrays as objects right off the bat with the var array = {} syntax, when really, to my understanding at least, declaring new Array does that anyway, but with a few added features. What interested me more in that case was how javascript iterated through such an object using "for x in object" syntax.

Link to comment
Share on other sites

It was discovered a while ago that associative arrays are dangerous in JS, that's why I said that you shouldnt use it. And even in your example, I get NAN or undefined. that is why i suggested using the object notation with for in loops. My bad on calling the variable array when it was clearly an object. However, everything in JS is essentially an object and can be accessed using either the dot or bracket notation, even functions/methods/classes.

 

You are correct, Length is not going ot work on an assoc array or object, but did you even try the code that i posted?

 

And I do not know why your count method doesnt work as expected, I could not reproduce the results you were receiving.

Link to comment
Share on other sites

Yeah, I was just sloppy with my example cause the specific application wasn't really the point. By the way, I didn't mean to sound snappy with my previous posting. I just got pissed at the failures of the language as compared to the much more robust PHP. Anyway, I think my original question was out of the scope of this forum and deals more with actual computer software inner workings rather than with Javascript as a language.

 

Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.