coffeescript - When does the "fat arrow" (=>) bind to "this" instance -


The fat arrow can be used in different settings but in any way I want that with the example Does not always bind.

fat arrow binds on 3 occasions

  1. While declaring the method
  2. While declaring the function in a global context,

    1 while declaring a method < / H3>

    When the Coffeescript compiler has the following synthetic pattern within a class declaration

      class A somemethod: (paramlist) = & gt;   

    This will give the following code within the creator of class A

      this.somemethod = __bind (this.somemethod, this);   

    Overwriting the initial assignment with the bound version of the definition function for that example

    2 When declaring a function within a method < P> When you define a function with a fat arrow inside a method, the coffeeplus compiler automatically creates one closure of the external method and shadow it variable _this Any questions for @ inside the internal function The reference will use the variable _this in the generated JavaScript code
      somemethod: - & gt; = & Gt; @someCall ()   

    and it is compatible javascript

      a. Protottoomomampan = function () {// _ reference this var _this = this; Return function () {// and _this is now used in internal function returns _this.someCall (); }; };   

    A definition of a function without a fat arrow does not stop it for you.

    3 When announcing any function in global context

    If you define a free floating function (which means that in a class as a method and any other function / Not within the method)

      Foo => & gt; @bar   

    Then the corresponding javascript will look like this

      var foo, _this = this; Foo = function () {Return _this.bar; };   

    Here again the interesting thing is that this is being assigned to < this foo this

    The important part is that this is always the global context of your execution environment. If you are in the browser this window will be an object. If you are running node.js, then it will be the modules you are running.

    Warning: You should not define any function to reach your global context. It does call for trouble.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -