So I have a string "getNumber":
I would like to use this string as a method for an object: myObj.getNumber()
Is that possible? Thanks
asked May 22, 2013 at 5:21
Jackie Chan
2,6726 gold badges36 silver badges71 bronze badges
2 Answers 2
As simple as:
myObj['getNumber']();
answered May 22, 2013 at 5:22
zerkms
256k73 gold badges447 silver badges551 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
There two ways to access value from js object .. one is dot notation . and other is square bracket notation [], which allows access to properties containing special characters and selection of properties using variables.
var key = 'getNumber';
myObj[key]();
More information on Mozilla, working with object guide.
answered May 22, 2013 at 5:25
rab
4,1641 gold badge32 silver badges42 bronze badges
Comments
lang-js