JS: storing function inside array and calling that function

We can store function inside the array and call it.

Ex.:

var arr = []; // note this creates array in js

var obk = {} // this creates empty ojbect

Ex.2:

var arr = [ “1”,

2,

false,

{ name = “James”,

address = “Mulholland Drive”

},

function(name){

var greeting = “hello “;

console.log(greeting+name);

}

]

Now we have different variables inside our array, we even have another array and function!

To call the stored function:

Note: to call the method use arr[4]()

arr[4](arr[3].name);  // this will get the name from the array under index 3 and pass it to the function

// output will be: hello James

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s