gpt4 book ai didi

javascript - 为什么 coffeescript 到处都使用 « return » 语句?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:41 26 4
gpt4 key购买 nike

写这样的东西时:

$(document).ready ->
doSomething()

doSomething = ->
alert('Nothing to do')

编译成

$(document).ready(function() {
return doSomething();
});

doSomething = function() {
return alert('Nothing to do');
};

在我的理解中,返回语句是针对值(字符串、数组、整数...)

为什么 coffeescript 这样做?

最佳答案

如果未指定,CoffeeScript 使用隐式返回。

CS 返回函数中最后一条语句的值。这意味着生成的 JS 将具有最后一条语句的值的 return,因为 JS 需要显式的 return

the return statement is for values (string, array, integer...)

是的,这些值可能会通过调用函数返回,例如您示例中的 doSomething()alert()。这些值是执行方法的结果并不重要。

关于javascript - 为什么 coffeescript 到处都使用 « return » 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15598727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com