Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling function inside object using bracket notation

If i have a function defined inside some object as in :

var myobject={
myfunction:function(){//mycode here}
}

usually you can access the function using:

myobject.myfunction()

but what if i want to use

myobject["myfunction"]

trying so , actually the function did not get called , how can i call the function using brackets notation ?

like image 768
ProllyGeek Avatar asked Dec 09 '22 10:12

ProllyGeek


1 Answers

Use it like. You were very close

myobject["myfunction"]();

You can also do this

var myfuncname="myfunction";
myobject[myfuncname]();
like image 128
Satpal Avatar answered Feb 13 '23 02:02

Satpal