I have an issue, where I want to create a 'hook' in javascript, so I created a small function to do it.
Here is my code:
GoogleMaps.prototype.callUserFunc = function( func, args ){
eval('GoogleMaps.' + func + '.apply(func, args)');
}
The issue I have is I really don't want to use the eval() function, is there any better way of doing this?
I apologise for the noobish question, my javascript is a little on the poor side.
Thanks!
You can use square bracket notation to access properties of an object.
example:
var x = {
someProperty: 5
};
console.log(x["someProperty"]); // => 5
console.log(x.someProperty); // => 5
var y = 'someProperty';
console.log(x[y]); // => 5
x["someProperty"] is identical to x.someProperty.
You can use it like this.
GoogleMaps.prototype.callUserFunc = function( func, args ){
GoogleMaps[func].apply(func, args);
}
GoogleMaps.prototype.callUserFunc = function( func, args ){
GoogleMaps[func].apply(func, args);
//eval('GoogleMaps.' + func + '.apply(func, args)');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With