1

I ran this code into the console and tried calling:

obj.foo().bar().coo().moo();

This was my previous code:

    function bar() {
  this.coo = function () {
    this.moo = function () {
      console.log("yay");
    }
  }
}

obj = {
  foo : function () {this.bar = bar}
};      

The error that returned was "TypeError: Cannot read property 'bar' of undefined". Although, when I wrote:

obj.foo();
obj.bar();
obj.coo();
obj.moo();

It worked fine. Why didn't the first call using method chaining not work as if I was separately calling each method.

1
  • 2
    you want to un-nest those methods... Commented Oct 18, 2014 at 19:38

1 Answer 1

4

You don't return anything. You need to add return this at the end of each of your functions in order to have an object to chain.

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

1 Comment

If this solved your problem, don't forget to hit the check mark to let others know that it was solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.