什么时候“胖箭头” (=>)结合到“这个”实例 [英] When does the "fat arrow" (=>) bind to "this" instance

查看:119
本文介绍了什么时候“胖箭头” (=>)结合到“这个”实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案


可以在不同的设置中使用,但不知道
总是绑定到我想要的实例。 div>

当声明方法时,脂肪箭头3次绑定


  1. 在声明全局上下文中的函数时,在方法中声明函数




1。当声明一个方法



当Coffeescript编译器在类声明中遇到以下语法模式

  class A 
somemethod:(paramlist)=>

这将在类A的构造函数中生成以下代码

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

这是该实例的定义覆盖初始赋值
的绑定版本函数



2。当在方法中声明一个函数



当在方法中使用胖箭头定义一个函数时,Coffeescript编译器
会自动创建一个闭包,并且阴影 this 输入到变量
_this 中。在内部函数中对 @ 的任何引用将使用生成的JavaScript代码中的变量 _this

  somemethod: - > 
=> @someCall()

这是相应的Javascript

  A.prototype.somemethod = function(){
// _引用此
var _this = this;
return function(){
//和_this现在在内部函数中使用
return _this.someCall();
};
};

没有胖箭头的函数定义不会为你创建闭包。



3。当在全局上下文中声明一个函数时



如果你定义一个自由浮动函数(意思是作为一个类中的方法而不是另一个函数/方法) p>

  foo = => @bar 

然后相应的Javascript将如下所示:

  var foo,
_this = this;

foo = function(){
return _this.bar;
};

这里有趣的是 em> 部分但是是总是你的执行环境的全局上下文。如果你在浏览器中,它将是窗口对象。如果你正在运行node.js,它将是你刚刚运行的模块。



警告:全球上下文。这需要麻烦。


The fat arrow can be used in different settings but it somehow doesn't always bind to the instance I want.

解决方案

The fat arrow binds at 3 occasions

  1. when declaring a method
  2. when declaring a function within a method
  3. when declaring a function in global context

1. When declaring a method

When the Coffeescript compiler encouters the following syntactical pattern within a class declaration

class A
    somemethod: (paramlist) =>

This will yield the following code within the constructor of class A

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

That is the definition for that instance is overwriting the initial assignment with a bound version of the function

2. When declaring a function within a method

When you define a function with a fat arrow within a method the Coffeescript compiler automatically creates a closure and and shadows this of the outer method into the variable _this. Any reference to @ within the inner function will use the variable _this in the generated javascript code

somemethod: ->
   => @someCall()

And this is the corresponding Javascript

A.prototype.somemethod = function() {
    //_this references this
    var _this = this;
    return function() {
        //and _this is now used within the inner function
        return _this.someCall();
    };
};

A definition of a function without the fat arrow doesn't create that closure for you.

3. When declaring a function in global context

If you define a free floating function (meaning as a method in a class and not within another function/method) just like this

foo = => @bar

Then the corresponding Javascript will look like this

var foo,
  _this = this;

foo = function() {
    return _this.bar;
};

The interesting thing here again is that this is being assigned to _this which enables the definition of foo to close over _this.

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

Warning: You shouldn't define any function accessing your global context anyway. This calls for trouble.

这篇关于什么时候“胖箭头” (=>)结合到“这个”实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆