Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

2 Answers

up vote 9 down vote accepted

As simple as:

myObj['getNumber']();
share|improve this answer
Lol an upvote a second :) – mplungjan 2 days ago

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.