2016-11-17 72 views
0

因此,我正在查看不是我的CoffeeScript代码,我想了解为什么类变量未定义。在CoffeeScript中运行时是1.9CoffeeScript类变量undefined 1.9

class CommandParser 

obj: 
    message: null 
    indicator: 'warning' 
    stackTrace: null 
    result: null 
    isException: false 

constructor: (@command, @params, @result) -> 
@obj.result = result //@obj is undefined 

我试图理解为什么@obj未定义

回答

0

假设你的压痕真的是这样的:

class CommandParser 
    obj: 
    message: null 
    indicator: 'warning' 
    stackTrace: null 
    result: null 
    isException: false 
    constructor: (@command, @params, @result) -> 
    @obj.result = result 

那么你没有得到一个TypeError为@obj正在undefined,因为没有result变量,您将收到ReferenceError。

当你说:

m: (@i) -> ... 

任何方法然后在参数列表中的@i该参数值自动给你的对象上@i实例变量,但不会有一个i局部变量。所以你constructor

constructor: (@command, @params, @result) -> 

当你调用它,但没有result局部变量仍遥不可及自动设置@command@params@result实例变量。如果你想看看result值,那么你就看着@result

constructor: (@command, @params, @result) -> 
    @obj.result = @result 
    # ------------^ 

,或者你会离开的@关闭参数列表:

constructor: (@command, @params, result) -> 
    @obj.result = result 

这就是明显的bug照顾的,隐藏的错误接下来。当你定义在类级别的东西:

class C 
    p: { a: 11 } 

然后pC的原型所以它是由C所有实例共享的一部分。在你的情况下,只有一个@obj将通过CommandParser所有实例共享的对象,所以如果你说:

c1 = new CommandParser('c1', 'p1', 'r1') 
c2 = new CommandParser('c2', 'p2', 'r2') 

那么这两个c1.obj.resultc2.obj.result因为他们使用完全相同的@obj参考这两个会'r2'

演示:https://jsfiddle.net/ambiguous/kffswpxm/

类级别定义可变值几乎总是错误的,在构造函数中定义它们,使每个实例都有自己:

class CommandParser 
    constructor: (@command, @params, @result) -> 
    @obj = 
     message: null 
     indicator: 'warning' 
     stackTrace: null 
     result: @result 
     isException: false 

演示:https://jsfiddle.net/ambiguous/k3kmg1cc/

如果你想在类级别文档的目的来定义他们,那么你要克隆它在构造函数:

class CommandParser 
    obj: 
    message: null 
    indicator: 'warning' 
    stackTrace: null 
    result: null 
    isException: false 
    constructor: (@command, @params, @result) -> 
    @obj = _(@obj).cloneDeep() 
    @obj.result = @result 

演示:https://jsfiddle.net/ambiguous/r69vood7/

这个例子使用cloneDeep from Lodash,有几乎每个实用带JavaScript库都有类似的克隆工具。