【问题标题】:use .call(this) in coffescript [duplicate]在咖啡脚本中使用 .call(this) [重复]
【发布时间】:2014-07-29 20:56:26
【问题描述】:

在编译 CoffeScript 以更好地理解它时,我开始想知道如何编写包装函数

(function(){

}).call(this)

使用 CoffeScript。

我知道我可以按照它应该的方式编译它,但我很好奇。

【问题讨论】:

    标签: javascript coffeescript


    【解决方案1】:

    编译:

    console.log('Try');
    

    在 CoffeeScript 1.6.3 中给出:

    // Generated by CoffeeScript 1.6.3
    (function() {
      console.log('Try');
    
    }).call(this);
    

    在这里试试:http://www.compileonline.com/try_coffeescript_online.php

    或者如果你想要一个裸机,编译:

    #blank
    

    给予:

    // Generated by CoffeeScript 1.6.3
    (function() {
    
    
    }).call(this);
    

    如果打开了 --bare 选项,您可以使用:

    ( -> ) .call(this)
    

    给出:

    (function() {}).call(this);
    

    在这里试试:http://coffeescript.org/(试试咖啡标签。)

    【讨论】:

    • 你可能想检查 -b 选项here
    • $coffee -c -p main.coffee 是我正在使用的
    • 是的,这行得通,但如果你添加 -b ($coffee -c -b -p main.coffee) 它会省略包装函数。这就是我正在做的事情。
    • 但这只有在你想在其中做一些事情时才有意义。
    • coffeescript.org - 如果您在此处看到,第一个示例会生成它。但仅仅是因为它被用作 IIFE。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    相关资源
    最近更新 更多