[ACCEPTED]-Passing a parameter as a pointer in JavaScript-pointers
You can achieve the effect you want by taking 2 advantage of the fact that Javascript passes 1 objects by reference:
var o = { };
(function (p) {
p.fn = function () {
alert('test');
};
})(o);
o.fn();
You're better off using the language as 4 intended rather than trying to bend it to 3 fit idioms that only make sense in a language 2 constructed according to a different set 1 of principles.
var o = (function(p) {
return function() {
alert('test');
};
})();
o();
When you pass in variable o in your example, you're 10 passing an undefined value, not a reference 9 to anything. What you'd need to do, is send 8 the objects parent. In this case, the window 7 object (assuming you're using a browser).
(function (p, name) {
p[name] = function() {
alert('test');
};
})(window, "o");
o();
This 6 passes in the window object, and the name 5 of your new function. Then it assigns your 4 function to the window, and it now available 3 to be called. Unless you're going to use 2 closures, though, I'd suggest just assigning 1 the function directly.
function o() {
alert('test');
};
o();
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.