admin管理员组文章数量:1426087
I am trying to do something similar to what Google Analytics is doing. I want to push a function name along with parameters into an array and then execute the function name along with the parameter.
For example:
var _test = _test || [];
_test.push(['setName', 'Todd']);
And execute setName in here:
var widget = function () {
function _private_setName(a) {
console.log(a);
}
return{
setName:_private_setName
};
}();
console.log(_test);
_test contains the function name and parameter, but how can i execute the function?
I am trying to do something similar to what Google Analytics is doing. I want to push a function name along with parameters into an array and then execute the function name along with the parameter.
For example:
var _test = _test || [];
_test.push(['setName', 'Todd']);
And execute setName in here:
var widget = function () {
function _private_setName(a) {
console.log(a);
}
return{
setName:_private_setName
};
}();
console.log(_test);
_test contains the function name and parameter, but how can i execute the function?
Share Improve this question asked Aug 20, 2013 at 8:55 AlosyiusAlosyius 9,15126 gold badges78 silver badges121 bronze badges2 Answers
Reset to default 6You have to iterate over the array. The first element of each inner array will be the method name, which you can use with bracket notation to access the method of the object. To pass the remaining elements as arguments to the method, use .apply
.
Example:
if (window._test) {
for (var i = 0; i < _test.length; i++) {
var method = _test[i].shift(); // this modifies the inner array!
widget[method].apply(widget, _test[i]);
}
}
Of course you should add some checks so that you try to call methods that don't exist.
To make this "pattern" really work you also have to handle calls to _test.push
properly after the script was loaded. As it is now, any elements that are added to the array after the script was loaded would simply be ignored.
One solution would be to replace _test
after you processed the existing _test
array with an object having a .push
method. Since widget
exists now, we can immediately execute the function call. For the calling code, the interface does not change.
// iterate over _test and process data
window._test = { // overwrite _test
push: function(cmd) {
var method = cmd.shift();
widget[method].apply(widget, cmd);
}
};
As you can see, this would only work if _test
is global. I think in Google Analytics, _gaq
has to be global as well.
The good aspect about this approach is that you don't have wait until widget
is ready. A drawback could be that the code is not as clear is just waiting for the script to load and then make "normal" method calls.
So, when would you use one or the other? Personally I would choose to simple wait until the script is loaded, e.g.
loadScript('scripturl', function() {
// script is ready, do stuff
});
However, there are cases where you cannot wait until a script was loaded. For example if you want to track user interaction (what Google Analytics does). If you also want to include the script dynamically, i.e. there is no script
tag for it in the HTML, you have to use some form of mand queue.
var _test = _test || [];
_test.push(['setName', 'Todd']);
_test.push(['setName', 'Joe']);
for(var i = 0; i < _test.length; i++){
widget[_test[i][0]](_test[i][1]);
}
本文标签: JavaScript push function and parameter to an array and executeStack Overflow
版权声明:本文标题:JavaScript push function and parameter to an array and execute? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745463016a2659409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论