admin管理员组文章数量:1401620
Sometimes we need to call a function by its name. I can do it in plain JavaScript as below:
global=this
function add(a,b){return a+b}
global['add'](1,2)
Which works as expected and add()
gets called.
Equivalent CoffeeScript code might can be written as below.
global=@
add=(a,b)->a+b
global['add'](1,2)
which piles to JavaScript as:
(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.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.
Is there a simple hack? Or any simpler solution?
Sometimes we need to call a function by its name. I can do it in plain JavaScript as below:
global=this
function add(a,b){return a+b}
global['add'](1,2)
Which works as expected and add()
gets called.
Equivalent CoffeeScript code might can be written as below.
global=@
add=(a,b)->a+b
global['add'](1,2)
which piles to JavaScript as:
(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.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.
Is there a simple hack? Or any simpler solution?
Share Improve this question edited Nov 28, 2012 at 11:41 JJJ 33.2k20 gold badges94 silver badges103 bronze badges asked Jun 28, 2011 at 12:03 CDRCDR 8,41811 gold badges49 silver badges46 bronze badges2 Answers
Reset to default 8Your add
is a local variable. Use
@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.
.
By default the CoffeeScript piler wraps every file in a closure scope, but you can disable this behavior by adding the --bare switch as piler option.
add.coffee:
add = (a,b) -> a+b
When you run: coffee --pile 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 --pile 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.
本文标签: javascriptCalling a function by its nameStack Overflow
版权声明:本文标题:javascript - Calling a function by its name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744285961a2598878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论