www.webdeveloper.com
Results 1 to 3 of 3

Thread: What does this javascript syntax do -> obj['name'](var1,var2)

  1. #1
    Join Date
    Jun 2012
    Posts
    10

    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);


    I understand
    Code:
    obj['name'] = "";
    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.

  2. #2
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    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:

    PHP Code:
    obj['add'] = function(xy) { return y; }
    var 
    sum obj.add(12);
    var 
    other_sum obj['add'](12); 
    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

  3. #3
    Join Date
    Jun 2012
    Posts
    10
    Thanks svidgen

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles