如何避免 CoffeeScript 方法变量被包装在对象文字中?

发布于 2025-01-07 16:24:37 字数 1585 浏览 1 评论 0原文

CoffeeScript 将方法内声明的变量包装在对象文字中。

所以,这样:

@Templates =
    get: (templateName) ->
        result: ''              # DECLARED HERE
        $.ajax(
            'Views/Templates/' + templateName + '.html',
            type: 'GET'
            dataType: 'html'
            success: (data) ->
                result = data   # ASSIGNED HERE
            async: false
        )
        return result           # RETURNED HERE

变成这样:

(function() {

  this.Templates = {
    get: function(templateName) {
      ({
        result: ''                  //DECLARED IN AN OBJECT LITERAL - I DON'T WANT THIS
      });
      $.ajax('Views/Templates/' + templateName + '.html', {
        type: 'GET',
        dataType: 'html',
        success: function(data) {
          var result;               //DECLARED LOCAL TO THE CALLBACK - I DON'T WANT THIS
          return result = data;
        },
        async: false
      });
      return result;                //RETURNED HERE - UNASSIGNED
    }
  };

}).call(this);

但我需要的,对我有用的是:

(function() {

  this.Templates = {
    get: function(templateName) {
      var result = ''               //DECLARED HERE
      $.ajax('Views/Templates/' + templateName + '.html', {
        type: 'GET',
        dataType: 'html',
        success: function(data) {
          return result = data;     //ASSIGNED HERE
        },
        async: false
      });
      return result;                //RETURNED HERE
    }
  };

}).call(this);

我做错了什么?我该如何解决这个问题?

CoffeeScript wraps variables declared inside methods in an object literal.

So, this:

@Templates =
    get: (templateName) ->
        result: ''              # DECLARED HERE
        $.ajax(
            'Views/Templates/' + templateName + '.html',
            type: 'GET'
            dataType: 'html'
            success: (data) ->
                result = data   # ASSIGNED HERE
            async: false
        )
        return result           # RETURNED HERE

Becomes this:

(function() {

  this.Templates = {
    get: function(templateName) {
      ({
        result: ''                  //DECLARED IN AN OBJECT LITERAL - I DON'T WANT THIS
      });
      $.ajax('Views/Templates/' + templateName + '.html', {
        type: 'GET',
        dataType: 'html',
        success: function(data) {
          var result;               //DECLARED LOCAL TO THE CALLBACK - I DON'T WANT THIS
          return result = data;
        },
        async: false
      });
      return result;                //RETURNED HERE - UNASSIGNED
    }
  };

}).call(this);

But what I need, and that works for me, is this:

(function() {

  this.Templates = {
    get: function(templateName) {
      var result = ''               //DECLARED HERE
      $.ajax('Views/Templates/' + templateName + '.html', {
        type: 'GET',
        dataType: 'html',
        success: function(data) {
          return result = data;     //ASSIGNED HERE
        },
        async: false
      });
      return result;                //RETURNED HERE
    }
  };

}).call(this);

What am I doing wrong? How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じее 2025-01-14 16:24:37

你所说的闭包并不是闭包(JavaScript 中的闭包始终是函数)。它是一个括在括号中的对象文字。

我对 CoffeeScript 不太熟悉,但是如果您希望 result 成为 get 函数中的局部变量,我相信您想要更改

result: ''

result = ''

前者是对象初始值设定项格式(因此它被转换为对象文字),后者是变量赋值。似乎在 CoffeeScript 网站的词法范围下有所介绍。

What you've called a closure isn't a closure (closures in JavaScript are always functions). It's an object literal wrapped in parentheses.

I'm not really au fait with CoffeeScript, but if you want result to be a local variable in the get function, I believe you want to change

result: ''

to

result = ''

The former is the object initializer format (hence it getting translated into an object literal), the latter is a variable assignment. Seems to be covered on the CoffeeScript site under lexical scoping.

反话 2025-01-14 16:24:37

通过 result: '' 您可以声明对象的属性。您需要的是通过 result = '' 声明一个局部变量:

get: (templateName) ->
    result = ''              # DECLARED HERE
    $.ajax(
        'Views/Templates/' + templateName + '.html',
        type: 'GET'
        dataType: 'html'
        success: (data) ->
            result = data   # ASSIGNED HERE
        async: false
    )
    return result           # RETURNED HERE

By result: '' you declare a property of an object. What you needed was to declare a local variable by result = '':

get: (templateName) ->
    result = ''              # DECLARED HERE
    $.ajax(
        'Views/Templates/' + templateName + '.html',
        type: 'GET'
        dataType: 'html'
        success: (data) ->
            result = data   # ASSIGNED HERE
        async: false
    )
    return result           # RETURNED HERE
晒暮凉 2025-01-14 16:24:37

为什么要使用 :?它没有被包装在闭包中,您只是定义了一个匿名对象表达式。

只需这样做:

result = ''

也就是说,为什么要执行同步 GET?这是非常不受欢迎的,并且会使您的网站响应速度降低。

Why are you using a :? It's not being wrapped in a closure, you are just defining an anonymous object expression.

Just do this:

result = ''

That said, why are you doing a synchronous GET? That's very frowned upon and will make your site must less responsive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文