Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Eval code issue

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!

like image 682
DarkMantis Avatar asked Jul 29 '26 02:07

DarkMantis


2 Answers

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);
}
like image 122
Nathan Wall Avatar answered Jul 30 '26 17:07

Nathan Wall


GoogleMaps.prototype.callUserFunc = function( func, args ){
  GoogleMaps[func].apply(func, args);
  //eval('GoogleMaps.' + func + '.apply(func, args)');
}
like image 33
Pheonix Avatar answered Jul 30 '26 17:07

Pheonix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!