CoffeeScript 1.9.0 对变量名称的更改
CoffeeScript 1.9.0 changes to variable names
在CoffeeScript 1.9.0 ChangeLog中,我读到:
Changed strategy for the generation of internal compiler variable names. Note that this means that @example
function parameters are no longer available as naked example
variables within the function body.
我不太明白这对我这个用户意味着什么。这是某种不兼容的更改吗?我可以安全地升级到 1.9.0 版吗?
视情况而定。是的,此更改是不兼容的。如果你有书面测试,你可以检查它是否影响你。拿这小段代码:
example = "new"
obj = method: (@example) -> console.log(example)
obj.method "old"
在 1.8 中,这将打印 old
。在新版本中,这会打印 new
.
在旧版本中,@example
将在方法参数中转换为 example
。所以你在旧版本中访问 obj.method
的函数参数。
在新版本中,您正在访问外部作用域的 example
变量。在这两种情况下,a.example
仍然设置为 "old"
。
这里可以看到生成的JS代码的区别:
-// 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);
请参阅 Patrick J. S. 的回答以了解更改的含义。
请参阅 了解如何知道您是否可以安全升级,以及如果不能安全升级您需要做什么。
在CoffeeScript 1.9.0 ChangeLog中,我读到:
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 版吗?
视情况而定。是的,此更改是不兼容的。如果你有书面测试,你可以检查它是否影响你。拿这小段代码:
example = "new"
obj = method: (@example) -> console.log(example)
obj.method "old"
在 1.8 中,这将打印 old
。在新版本中,这会打印 new
.
在旧版本中,@example
将在方法参数中转换为 example
。所以你在旧版本中访问 obj.method
的函数参数。
在新版本中,您正在访问外部作用域的 example
变量。在这两种情况下,a.example
仍然设置为 "old"
。
这里可以看到生成的JS代码的区别:
-// 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);
请参阅 Patrick J. S. 的回答以了解更改的含义。
请参阅