在嵌套函数中引用类对象时出现问题 [英] Trouble referencing class object inside a nested function

查看:124
本文介绍了在嵌套函数中引用类对象时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在coffeescript中创建一个类,我几乎在那里。我的问题是,我想为类的整个范围创建一对夫妇的变量,但我不知道如何到嵌套函数内。 @等效于this。,但是我想能够从类中的任何地方获得这些构造函数的属性。



示例:

  class CoffeeScriptClass 
constructor:(@foo) - >

_sampleFunction: - >
$ .each BigArray,(index,NestedArray) - >
$ .each NestedArray,(index,widget) - >

##我想@foo引用构造函数值

widget = @foo

return
return
return

这是否有意义?我真的想保持我的OO Javascript整洁和有组织,但我有一个困难的时间与咖啡的范围部分。我很高兴地欢迎任何重构/建议在我的课余下。

解决方案

您需要限定内部函数的范围:

  class CoffeeScriptClass 
constructor:(@foo) - >

_sampleFunction: - >
$ .each BigArray,(index,NestedArray)=> //保持父作用域
$ .each NestedArray,(index,widget)=> //保持父作用域

##我想要@foo引用构造函数值

widget = @foo

return
return
return






编译的JS显示为什么这样工作:

  var CoffeeScriptClass; 

CoffeeScriptClass =(function(){

function CoffeeScriptClass(foo){
this.foo = foo;
}

CoffeeScriptClass.prototype._sampleFunction = function(){

var _this = this; //维护对类的引用

$ .each(BigArray,function ,NestedArray){
$ .each(NestedArray,function(index,widget){
widget = _this.foo;
});
});
} ;

return CoffeeScriptClass;

})();


I'm trying to create a class in coffeescript and I'm almost there. My problem is, I'd like to create a couple of variables for the entire scope of the class, however I don't know how to get to them inside nested functions. @ is equivalent to "this.", however I'd like to be able to get to those constructor properties from anywhere inside the class.

Example:

class CoffeeScriptClass
  constructor: (@foo) ->

  _sampleFunction: ->
    $.each BigArray, (index, NestedArray) ->
      $.each NestedArray, (index, widget) ->

        ## I'd like @foo to reference the constructor value

        widget = @foo 

        return
      return  
    return

Does this make sense? I'm really trying to keep my OO Javascript tidy and organized, but I'm having a difficult time with the scoping part of coffeescript. I'll happily welcome any refactoring/advice on the rest of my class. Thanks all.

解决方案

You need to scope the inner functions:

class CoffeeScriptClass
  constructor: (@foo) ->

  _sampleFunction: ->
    $.each BigArray, (index, NestedArray) => // keep parent scope
      $.each NestedArray, (index, widget) => // keep parent scope

        ## I'd like @foo to reference the constructor value

        widget = @foo 

        return
      return  
    return


Here is the compiled JS showing why this works:

var CoffeeScriptClass;

CoffeeScriptClass = (function() {

  function CoffeeScriptClass(foo) {
    this.foo = foo;
  }

  CoffeeScriptClass.prototype._sampleFunction = function() {

    var _this = this; // reference to the class is maintained

    $.each(BigArray, function(index, NestedArray) {
      $.each(NestedArray, function(index, widget) {
        widget = _this.foo;
      });
    });
  };

  return CoffeeScriptClass;

})();

这篇关于在嵌套函数中引用类对象时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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