Sometimes we need to call a function by its name. I can do it in plain JavaScript as below:
有时我们需要通过名称来调用函数。我可以用普通的JavaScript做到,如下所示:
global=this
function add(a,b){return a+b}
global['add'](1,2)
Which works as expected and add()
gets called.
哪个工作正常,add()被调用。
Equivalent CoffeeScript code might can be written as below.
等效的CoffeeScript代码可以编写如下。
global=@
add=(a,b)->a+b
global['add'](1,2)
which compiles to JavaScript as:
编译为JavaScript的:
(function() {
var add, global;
global = this;
add = function(a, b) {
return a + b;
};
global['add'](1, 2);
}).call(this);
...and it doesn't work.
......它不起作用。
Microsoft JScript runtime error: Object doesn't support this property or method
Is there an easy solution to this problem?
这个问题有一个简单的解决方案吗?
Note:
-
I am not running the code in a browser therefore there is no window object. But in plain JS I can always capture the global scope by assigning
global=this
and then get the function pointer from it.我没有在浏览器中运行代码,因此没有窗口对象。但是在简单的JS中,我总是可以通过赋值global = this捕获全局范围,然后从中获取函数指针。
-
One solution in CoffeeScript I found is by declaring all functions as member of a global object like
global.add=[function definition]
. But then I have to normally call the function asglobal.add()
. And it's more boiler plate than necessary.我发现CoffeeScript中的一个解决方案是将所有函数声明为全局对象的成员,如global.add = [function definition]。但是我必须通常将该函数称为global.add()。它比锅炉板更多。
Is there a simple hack? Or any simpler solution?
有一个简单的黑客?或者更简单的解决方案?
2 个解决方案
#1
8
Your add
is a local variable. Use
你的add是一个局部变量。使用
@add=(a,b)->a+b
to attach it to the global
object. Since global
is the global scope of your script you can still call add
without prefixing it with global.
.
将它附加到全局对象。由于global是脚本的全局范围,因此您仍然可以调用add而不使用global作为前缀。
#2
0
By default the CoffeeScript compiler wraps every file in a closure scope, but you can disable this behavior by adding the --bare switch as compiler option.
默认情况下,CoffeeScript编译器会封装闭包范围中的每个文件,但您可以通过将--bare开关添加为编译器选项来禁用此行为。
add.coffee:
add = (a,b) -> a+b
When you run: coffee --compile add.coffee
当你运行:coffee --compile add.coffee
That will produce:
这会产生:
(function() {
var add;
add = function(a,b) {
return a+b;
};
}).call(this);
Now try run it with the --bare switch coffee --bare --compile add.coffee
现在尝试使用--bare switch coffee --bare --compile add.coffee运行它
That will produce:
这会产生:
var add;
add = function(a, b) {
return a + b;
};
Now you file is not closure scoped, and you can do it the way you did it before.
现在你的文件不是封闭范围,你可以像以前那样做。
#1
8
Your add
is a local variable. Use
你的add是一个局部变量。使用
@add=(a,b)->a+b
to attach it to the global
object. Since global
is the global scope of your script you can still call add
without prefixing it with global.
.
将它附加到全局对象。由于global是脚本的全局范围,因此您仍然可以调用add而不使用global作为前缀。
#2
0
By default the CoffeeScript compiler wraps every file in a closure scope, but you can disable this behavior by adding the --bare switch as compiler option.
默认情况下,CoffeeScript编译器会封装闭包范围中的每个文件,但您可以通过将--bare开关添加为编译器选项来禁用此行为。
add.coffee:
add = (a,b) -> a+b
When you run: coffee --compile add.coffee
当你运行:coffee --compile add.coffee
That will produce:
这会产生:
(function() {
var add;
add = function(a,b) {
return a+b;
};
}).call(this);
Now try run it with the --bare switch coffee --bare --compile add.coffee
现在尝试使用--bare switch coffee --bare --compile add.coffee运行它
That will produce:
这会产生:
var add;
add = function(a, b) {
return a + b;
};
Now you file is not closure scoped, and you can do it the way you did it before.
现在你的文件不是封闭范围,你可以像以前那样做。