/    Sign up×
Community /Pin to ProfileBookmark

What does this javascript syntax do -> obj[‘name’](var1,var2)

What does this line of code in javascript execute?

[CODE]return obj[‘name’](var1,var2);[/CODE]

I understand

[CODE]obj[‘name’] = “”;[/CODE]

is an associative array with a value. but what happens when there are parenthesis and passing arguments next to it? and how would i debug it in the web developer console? Thanks for any help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@svidgenAug 02.2012 — A function can behave like any other object in javascript. It can be assigned to a variable, popped into an array, or stuffed in an object:

[code=php]obj['add'] = function(x, y) { return x + y; }
var sum = obj.add(1, 2);
var other_sum = obj['add'](1, 2);[/code]


At the console in Chrome (or Safari):

[CODE]> var obj = {};
undefined
> obj['add'] = function(x, y) { return x + y; }
function (x, y) { return x + y; }
> obj.add;
function (x, y) { return x + y; }
> obj['add'];
function (x, y) { return x + y; }
> obj.add(1, 2);
3
> obj['add'](3, 4);
7[/CODE]
Copy linkTweet thisAlerts:
@anthoangauthorAug 02.2012 — Thanks svidgen

having a hard time trying to find in the code where this function is being executed to get the return value.
×

Success!

Help @anthoang spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...