0

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

2 Answers 2

9

As simple as:

myObj['getNumber']();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.