Jump to content

Array.prototype


ShadowMage

Recommended Posts

Hey guys, I have a question once again. (Big shocker there! :) )I'm trying to create a new function for array objects using the prototype attribute. I have it set up this way:Array.prototype.merge = function() { ... }...but when I tried to test it out with the following code:

var arrOne = ("one", "two", "three");var arrTwo = (1, 2, 3);var newArr = arrOne.merge(arrTwo);alert(newArr);

I get an error in Firebug saying that arrOne.merge is not a function. How do I get this to work? Am I creating the function properly?Thanks in advance.

Link to comment
Share on other sites

The merge function is not complete yet. I'm just testing as I go to make sure I get it the way I want it. Here's what I have though:

Array.prototype.merge = function() {	var arrResult = this;		var bound = arguments.length;	for (x=0; x<bound; x++) {		arrResult = arrResult.concat(arguments[x]);	}	return arrResult;}

Link to comment
Share on other sites

I just realized you're not initializing your arrays properly:

var arrOne = ("one", "two", "three");var arrTwo = (1, 2, 3);

You should be using square brackets.

var arrOne = ["one", "two", "three"];var arrTwo = [1, 2, 3];

Link to comment
Share on other sites

have you checked to see what the arguments look like when printed out? maybe alert(arguments)?

Link to comment
Share on other sites

Could lead to problems. Builtin methods and properties don't show up as object elements, but re-prototyped elements do. So you may find your merge method showing up in the result of the merge!This is not a problem for numerically indexed arrays, but some techniques used on associative arrays can blow up when you mess with the prototype. Foreach is the classic example. I'm not saying don't do it. Just to be aware of the consequences.

Link to comment
Share on other sites

I just realized you're not initializing your arrays properly:
var arrOne = ("one", "two", "three");var arrTwo = (1, 2, 3);

You should be using square brackets.

var arrOne = ["one", "two", "three"];var arrTwo = [1, 2, 3];

Doh! Yeah I just realized that too. At least I'm not the only one who didn't see it though! :)Thanks guys!
Link to comment
Share on other sites

Could lead to problems. Builtin methods and properties don't show up as object elements, but re-prototyped elements do. So you may find your merge method showing up in the result of the merge!This is not a problem for numerically indexed arrays, but some techniques used on associative arrays can blow up when you mess with the prototype. Foreach is the classic example. I'm not saying don't do it. Just to be aware of the consequences.
Thanks, that's good to know. :)
Link to comment
Share on other sites

Doh! Yeah I just realized that too. At least I'm not the only one who didn't see it though! :)Thanks guys!
you would have if you tried printing them out :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...