CoffeeScript 1.9.0更改为变量名 [英] CoffeeScript 1.9.0 changes to variable names
问题描述
在 CoffeeScript 1.9.0 ChangeLog 中,我阅读了以下内容:
In the CoffeeScript 1.9.0 ChangeLog, I read:
更改了生成内部编译器变量名称的策略。注意,这意味着
@example
函数参数不再作为裸体示例
在函数体内可用。 p>
Changed strategy for the generation of internal compiler variable names. Note that this means that
@example
function parameters are no longer available as nakedexample
variables within the function body.
我不太明白这对我来说意味着什么。这是不可避免的变化吗?我可以安全升级到版本1.9.0吗?
I don't quite understand what this means for me as a user. Is this somehow an incompatible change? Can I safely upgrade to version 1.9.0?
推荐答案
是,此更改不兼容。如果你有书面测试,你可以检查它是否影响你。取这小段代码:
It depends. Yes, this change is incompatible. If you had written tests, you could check if it affects you. Take this little piece of code:
example = "new"
obj = method: (@example) -> console.log(example)
obj.method "old"
old
。在新版本中,这将打印 new
。
In 1.8 this would print old
. In the new version, this prints new
.
在旧版本中, @example
将在方法参数中转换为 example
。因此,您在旧版本中访问 obj.method
的函数参数。
在新版本中,您正在访问 example
变量。 a.example
在这两种情况下仍设置为old
。
In the older version, @example
would be translated to example
in the method parameters. So you're accessing obj.method
's function parameter in the old version.
In the new version you're accessing the example
variable of the outer scope. a.example
still gets set to "old"
in both cases.
在这里您可以看到生成的JS代码的区别:
Here you can see the difference in the generated JS code:
-// Generated by CoffeeScript 1.7.1
+// Generated by CoffeeScript 1.9.0
(function() {
var example, obj;
example = "new";
obj = {
- method: function(example) {
- this.example = example;
+ method: function(_at_example) {
+ this.example = _at_example;
return console.log(example);
}
};
obj.method("old");
}).call(this);
这篇关于CoffeeScript 1.9.0更改为变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!