43

I know the title is vague but I didn't know what to write.
In javascript, I know how to write functions that will be called like this :

argument1.function(argument2);

Here is the fiddle demonstration : http://jsfiddle.net/rFXhf/
Now I wonder if I can do :

argument1.argument2.function(argument3);//And even more!
0

2 Answers 2

78

Modern ES6 Approach

You no longer need to specify the function keyword when defining functions inside objects.

First option with named functions:

const myObj = {
  myMethod(params) {
    // ...do something here
  },
  myOtherMethod(params) {
    // ...do something here
  },
  nestedObj: {
    myNestedMethod(params) {
      // ...do something here
    }
  }
};

Second option with anonymous functions:

const myObj = {
  myMethod: (params) => {
    // ...do something here
  },
  myOtherMethod: (params) => {
    // ...do something here
  },
  nestedObj: {
    myNestedMethod: (params) => {
      // ...do something here
    }
  }
};

Pre ES6 style:

const myObj = {
  myMethod: function myMethod(params) {
    // ...do something here
  },
  myOtherMethod: function myOtherMethod(params) {
    // ...do something here
  },
  nestedObj: {
    myNestedMethod: function myNestedMethod(params) {
      // ...do something here
    }
  }
}; 

Note: In the first example the functions are named and have their own this-context. In the second example, the this-context from outside of the functions is used. In the third example, the functions are not named, but have their own this-context.

So while all methods seem similar, they are a bit different.

Sign up to request clarification or add additional context in comments.

3 Comments

this makes more sense to me when using ES6 myMethod: params => { // ...do something here }
@Rubanov. Using the arrow function is not equivalent to the above. The context of your myMethod would result in an undefined or window context. Mozilla doc on JavaScript Arrow functions states, "arrow function expressions are best suited for non-method functions." for this reason. ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
Be careful with 2nd option because arrow functions' this is not bound to the immediate enclosing object. So MDN said "arrow function expressions are best suited for non-method functions" developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48

you need to define the objects like this :

var argument1 = {
    myvar : "12",
    mymethod : function(test) { return something; }
}

then call mymethod like:

argument1.mymethod(parameter);

or the deeper version :

var argument1 = {
    argument2 : {
       mymethod : function(test) { return something; }
    }
} 

then:

argument1.argument2.mymethod(parameter);

5 Comments

This would work but i want a version like the jsfiddle where you just write : var a;var b;a.pow(b); for exemple
do you have a specific type of arguments or you want it to work on anything?
Ok, now I got it. I don't think you can do that because "." connector is for objects reaching its sub functions/variables.
Is that not the same a myMethod(argument1, argument2, parameter) ?
@sujumayas no it isn't. you can't access argument1 argument2 in the nested method. You don't need to. Think it like containers of functions. one object can contain any type of nodes, so you nest it with inner objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.