Method chaining using javascript not working?

Unknown

Im trying to get method chaining to work using javascript:

(function(){
    var first = function(){
        console.log("1st Chain");
        return this;
    };

    var second = function(){
        console.log("2nd Chain");
    }

    first().second();
})();

Only the first function is printing to the console but the second function is saying its undefined. However when I tried it using a Constructor, it works.

var Chaining = function(){
   this.first = function(){
       console.log("1st Chain");
       return this;
   };

   this.second = function(){
       console.log("2nd Chain");
   };
};

var chain = new Chaining;
chain.first().second(); // this works fine.
pce

In the first Chain this is bound to the window and therefore second is not visible (undefined in that scope).

(function(){

    var first = function(){
        console.log("1st Chain");
        console.log(this);
        return this;
    };

    window.second = function(){
        console.log("2nd Chain");
    }

    first().second();
})();

To get rid of an export to the global window object of the second function, you could use a Object that's only visible in the Scope of the anonymous function.

(function(){

    var Chain = {};

    Chain.first = function(){
        console.log("1st Chain");
        console.log(this);
        return this;
    };

    Chain.second = function(){
        console.log("2nd Chain");
        return this;
    }

    // example
    Chain.first().second().first();

    // example export 
    // window.Chain = Chain;

})();

If you want to chain in another scope, you could export your Chain to the window and use it without new, ie. Chain.first().second();.

You could also save a pointer to this (as pointed out by Jonathon):

(function(){

    var self = this;

    this.first = function(){
        console.log("1st Chain");
        console.log(this);
        return self;
    };

    this.second = function(){
        console.log("2nd Chain");
        return self;
    }

    // example
    first().second().first();

    // export example
    // window.Chain = self;
})();

// exported anonymous function to Chain    
// Chain.first().second();

Saving a pointer to this is sometimes necessary, because the this keyword is scope-changing and refers to the context of the executed function. this does not refer to the instance of the object, where this is used, like you would expect in Java.

Every function can therefore bound to another scope. To adjust the function context use bind, apply and call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Method chaining using javascript not working?

From Dev

Javascript method chaining working but convoluted

From Dev

chaining async method calls - javascript

From Dev

JavaScript Method Chaining or Angular $q

From Dev

Redirect Using PHP Method Chaining?

From Dev

Chaining method calls using RxJava

From Dev

Generic type inference not working with method chaining?

From Dev

How to sleep a method in javascript method chaining

From Dev

How to sleep a method in javascript method chaining

From Dev

Recursive method not working correctly using JavaScript

From Dev

JavaScript function chaining using the singleton pattern

From Dev

JavaScript function chaining using the singleton pattern

From Dev

How to call the same JavaScript function repeatedly while waiting for the function to run its course before invoking it again, using method chaining?

From Dev

replacing innerHTML using replace method along with regular expressions not working in Javascript

From Dev

Method chaining with the same method

From Dev

How do I Apply chaining method on javascript document.createElement

From Dev

Javascript - Can indentation on method chaining cause issues with semicolons?

From Dev

Is it possible to start method chaining on a new line after a variable name in JavaScript?

From Dev

javascript first() method not working

From Dev

Call method is not working in javascript

From Dev

javascript method not working in chrome

From Java

How to use groupby transform across columns using method chaining?

From Dev

how to do logging of method chaining using spring aop

From Dev

how to do logging of method chaining using spring aop

From Dev

Passing 'this' context within method chaining (using apply / call or bind)

From Dev

.on("hover", ...) chaining not working

From Dev

ContinueWith chaining not working as expected

From Dev

Chaining not working on jQuery Plugin

From Dev

Optional Chaining Not Working As Expected

Related Related

  1. 1

    Method chaining using javascript not working?

  2. 2

    Javascript method chaining working but convoluted

  3. 3

    chaining async method calls - javascript

  4. 4

    JavaScript Method Chaining or Angular $q

  5. 5

    Redirect Using PHP Method Chaining?

  6. 6

    Chaining method calls using RxJava

  7. 7

    Generic type inference not working with method chaining?

  8. 8

    How to sleep a method in javascript method chaining

  9. 9

    How to sleep a method in javascript method chaining

  10. 10

    Recursive method not working correctly using JavaScript

  11. 11

    JavaScript function chaining using the singleton pattern

  12. 12

    JavaScript function chaining using the singleton pattern

  13. 13

    How to call the same JavaScript function repeatedly while waiting for the function to run its course before invoking it again, using method chaining?

  14. 14

    replacing innerHTML using replace method along with regular expressions not working in Javascript

  15. 15

    Method chaining with the same method

  16. 16

    How do I Apply chaining method on javascript document.createElement

  17. 17

    Javascript - Can indentation on method chaining cause issues with semicolons?

  18. 18

    Is it possible to start method chaining on a new line after a variable name in JavaScript?

  19. 19

    javascript first() method not working

  20. 20

    Call method is not working in javascript

  21. 21

    javascript method not working in chrome

  22. 22

    How to use groupby transform across columns using method chaining?

  23. 23

    how to do logging of method chaining using spring aop

  24. 24

    how to do logging of method chaining using spring aop

  25. 25

    Passing 'this' context within method chaining (using apply / call or bind)

  26. 26

    .on("hover", ...) chaining not working

  27. 27

    ContinueWith chaining not working as expected

  28. 28

    Chaining not working on jQuery Plugin

  29. 29

    Optional Chaining Not Working As Expected

HotTag

Archive