arrayfromargs() question

Feb 27, 2013 at 8:52pm

arrayfromargs() question

I can’t find anything on this and I was wondering:

calling arrayfromargs(), what is actually returned? If I do
var myAwesomeArray = arrayfromargs(arguments); is “myAwesomeArray” a reference to “arrayfromargs” or is it the equivalent of doing something like:
myAwesomeArray = myOtherNoteQuiteAsAwesomeArray.slice();

Hope someone can clarify or point me to the documentation.
Thanks!

#66757
Feb 27, 2013 at 9:37pm

It creates a new Array object, copying the content of arguments. The arguments object is an Array object, though it supports array style indexing. This is a convenience function so that people who want to call Array object methods can do so, without having to do that copying to an actual Array object in JS.

#240259
Feb 27, 2013 at 10:24pm

If I understand correctly that means that:

arrayfromargs() creates a new array (by copying, not reference) of “arguments”. this means when I assign it to something like newArray[0] = arrayfromargs(arguments); newArray[0] is now referencing the array created by arrayfromargs(). If I send another list to my function, a new array is created and, let’s say put in newArray[1], but since everything is done by copying already, [0] and [1] are pointing to two different arrays in memory.

Am I understanding this correctly?
Thanks!!

#240260
Feb 27, 2013 at 11:02pm

Yes. Please let us know if you experience otherwise.

However, please note that in JS array copying is not “deep” so if you have an array of arrays, only the top layer is distinct. Hopefully that doesn’t lead to more confusion, but it’s a general JS characteristic and plenty of more information on the subject online. e.g. quick google on “array deep copy in javascript”

http://stackoverflow.com/questions/597588/how-do-you-clone-an-array-of-objects-in-javascript

#240261
Feb 28, 2013 at 6:40pm

Thanks!

#240262
Jan 29, 2014 at 10:10am

eek, so just realised that calling this as a matter of course imediately doubles gc work on every call…. glad i found this

#279440

You must be logged in to reply to this topic.