Passing links to JS inside a function
In the following code, I am passing
(function(Index){
Index.Index = function(){console.log(23)};
Index = function(){console.log(123)};
}(Window.Index = Window.Index || {}));
But the return I get is Window.Index={Index:function...}
What magazines 23.
I am trying to have it re-declared as a function. for example, the expected value should be:
Window.Index = function(){console.log(123)};
What am I doing wrong?
source to share
The variable you get in your function is a reference to an object Index
, not a variable that contains that reference. In javascript, arguments are always passed by value, even if those values are references in the case of objects.
Solutions will
1) pass window
(i.e. the object containing the property Index
)
(function(holder){
holder.Index = function(){console.log(123)};
})(window);
2) to directly change window.Index:
(function(){
window.Index = function(){console.log(123)}; // or omit 'window', that the same
})();
3) to pass the name of the property:
(function(holder, propname){
holder[propname] = function(){console.log(123)};
})(window, "Index");
4) to pass the callback:
(function(callback){
var Index = function(){console.log(123)};
callback(Index);
})(function(v){window.Index=v});
Please note that you are using a regular template.
source to share