coffeescript对象的其他属性中的变量赋值对属性赋值的范围 [英] coffeescript scope of variable assignment vs property assignment in object's other properties

查看:120
本文介绍了coffeescript对象的其他属性中的变量赋值对属性赋值的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Ubersicht 撰写一些小工具。它使用一个node.js服务器并将每个 .coffee 文件视为一个独立的widget对象。我有问题定义常量设置在一个文件中使用。目前我知道有两种方法在文件的顶部定义这种类型的常量。

 #Way 1 
foo_1 = true
bar_1 = false
#Way 2
foo_2:true
bar_2:false

在同一个文件中,一个属性被赋值为一个字符串或一个函数。以上两种定义选项的方法仅在两种属性类型之一中有效。

  staticProperty: #{foo_1}在这里工作
但#{foo_2}的输出不工作


methodProperty:(input) - >
if foo_1#Raises foo_1未定义
如果@ foo_1#foo_1未定义,这是预期的
如果@ foo_2#这工作正常

我理解,方式2添加到对象的属性,但我不太确定如何1分配的工作原理,该文件本质上定义一个对象。你能解释一下吗?



还有一种方法可以定义一个可以从这两个地方访问的变量?

解决方案

我们将看一个很丑的例子来看看发生了什么:

  class C 
a:6
b:@ :: a
c = 11
d:c
@e = 23
f:@e
g: - > @a
h: - > C :: b
i: - > c
j: - > @ constructor.e

a 旧属性,在JavaScript中它看起来像:

  C.prototype.a = 6; 

b 附接到原型;这里:

  b:@ :: a 

@ 是类本身,因此在JavaScript中是:

  C.prototype.b = C.prototype.a 

一切都很好。



c 是一个私有变量。在JavaScript中它看起来像这样:

  var C =(function(){
function C(){}
var c = 11;
// ...
})();

我在这里包含更多的JavaScript上下文,所以你可以看到 c 的范围。 c 对 C 定义中的任何内容都可见,但无处可见。



d 是原型上的另一个属性,在JavaScript中看起来像这样:

  C.prototype.d = c 

用于构建类的包装器,因此 var c = 11 在此处可见。



e 是一个类属性,在JavaScript中只是:

  Ce = 23; 

f 是原型上的另一个属性。 @ 是这个上下文中的类本身(就像 b ):

  f:@e 

可以得到 e @e ,JavaScript看起来像:

  C.prototype.f = Ce; 

g h 方法应该很清楚。 i 方法起作用,因为它是SIF中用于定义 C 的闭包:

  C.prototype.i = function(){return c; }; 

j 方法起作用,因为它使用标准 构造函数属性以返回 C 本身。



演示: http://jsfiddle.net/ambiguous/tg8krgh2/






根据您的情况应用所有这一切,

  class Pancakes 
foo_1 = true
foo_2:true

我们看到,正确:

  staticProperty:输出#{foo_1}在这里工作
,#{@ :: foo_2}


methodProperty:(input) - >
#foo_1应该在这里工作得很好。
#@ foo_1未定义,这是预期的
#@ foo_2工作正常

我不知道为什么你在 methodProperty 里引用 foo_1 的问题,它应该工作正常, a href =http://coffeescript.org/#try:class%20C%0A%20%20p%20%3D%20true%0A%20%20m%3A%20-%3E%20console.log('hi ')%20if(p)%0A%0Ac%20%3D%20new%20C%0Ac.m()rel =nofollow>用于当前版本的CoffeeScript。

I'm writing some widgets for Ubersicht. It uses a node.js server and treats each .coffee file as a standalone widget object. I'm having issues defining constant settings to be used throughout one file. Currently I know of two ways to define this type of constant at the top of the file.

# Way 1
foo_1 = true
bar_1 = false
# Way 2
foo_2: true
bar_2: false

Further down in the same file either a property is assigned as a string or as a function. Each of the above two ways of defining an option only works in one of the two property types.

staticProperty: """Output #{foo_1} works here
but output of #{foo_2} doesn't work
"""

methodProperty: (input) -> 
  if foo_1 # Raises foo_1 is not defined
  if @foo_1 # foo_1 is undefined which is expected
  if @foo_2 # This works fine

I understand that way 2 add to the object's properties, but I'm not too sure how the way 1 assignment works given that the file is essentially defining an object. Can you explain this?

Also is there a way to define a variable that can be accessed from both places?

解决方案

We'll look at a big ugly example to see what's going on:

class C
    a: 6
    b: @::a
    c = 11
    d: c
    @e = 23
    f: @e
    g: -> @a
    h: -> C::b
    i: -> c
    j: -> @constructor.e

a is a normal old property, in JavaScript it looks like:

C.prototype.a = 6;

b is also a normal old property that is attached to the prototype; here:

b: @::a

@ is the class itself so in JavaScript this is:

C.prototype.b = C.prototype.a

and everything works just fine.

c is sort of a private variable. In JavaScript it looks like this:

var C = (function() {
  function C() {}
  var c = 11;
  //...
})();

I've included more JavaScript context here so that you can see c's scope. c is visible to anything inside the definition of C but nowhere else.

d is another property that is on the prototype and looks like this in JavaScript:

C.prototype.d = c

This assignment happens inside the SIF wrapper that is used to build the class so var c = 11 is visible here.

e is a class property and in JavaScript is just:

C.e = 23;

f is another property on the prototype. @ is the class itself in this context (just like in b):

f: @e

so we can get at e as @e and the JavaScript looks like:

C.prototype.f = C.e;

The g and h methods should be pretty clear. The i method works because it is a closure inside the SIF that is used to define C:

C.prototype.i = function() { return c; };

The j method works because it uses the standard constructor property to get back to C itself.

Demo: http://jsfiddle.net/ambiguous/tg8krgh2/


Applying all that to your situation,

class Pancakes
  foo_1 = true
  foo_2: true

We see that you can use either approach if you reference things properly:

staticProperty: """Output #{foo_1} works here
and so does #{@::foo_2}
"""

methodProperty: (input) -> 
  # foo_1 should work fine in here.
  # @foo_1 is undefined which is expected
  # @foo_2 works fine

I'm not sure why you're having a problem referencing foo_1 inside your methodProperty, it should work fine and does work fine with the current version of CoffeeScript.

这篇关于coffeescript对象的其他属性中的变量赋值对属性赋值的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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