ironSteel ironSteel - 2 months ago 16
Javascript Question

How do I transfer and replicate all the contents of a function into an array from the function itself?

So to be very clear. I want the execution of the function to cause the function itself to be added into the array. I want the function to be transferred over to the array from within the function..

Example:

emptyArray = [] //All functions will be sent here.

var transferFunction = function() {
alert("I want to be sent to emptyArray from
within the function itself. How do I do that?
I don't want to use emptyArray[0] = transferFunction;
What I want, is for transferFunction to replicate
itself, and be sent to the empty array.
Can anyone answer how to do this?");
}

Answer

Are you looking to do something like this?

var transferFunction = (function(arr){
    var _id = 10;
    var _fn = function(name) { console.log("Hi " + name); };
    return (arr[_id] = _fn);
})(window.emptyArray || (window.emptyArray = []));

// should log "Hi Sue"
transferFunction("Sue");

// should log "Hi Sam"
window.emptyArray[10]("Sam");