在 JavaScript 中使用块的返回值

发布于 2024-12-22 16:24:34 字数 743 浏览 0 评论 0原文

在我测试过的很多浏览器上,JavaScript 块实际上返回一个值。您可以在任何控制台中测试它:

for(var i = 0; i < 10; i++) {
    var sqrt = Math.sqrt(i);
    if(Math.floor(sqrt) === sqrt) {
        i;
    }
}

“返回”值是最后一个平方数,即9!但因为我想这不是一个表达式,所以你不能这样做:

for(var i = 0; i < 10; i++) {
    ...
} + 5

那不起作用。当然,它给出 + 55,因为它是一个单独的语句。将循环放在括号中显然会失败,并且如果块位于括号中(例如 ({f(); r}) - 不起作用),它将被视为对象并引发语法错误。

利用返回值的一种方法是使用 eval

eval('for(var i = 0; i < 10; i++) {var sqrt = Math.sqrt(i);if(Math.floor(sqrt) === sqrt) {i;}}') + 5; // 14

但如果 eval 是唯一的解决方案,我显然不想使用它。有没有办法使用块的结果值而不使用我缺少的 eval ?我真的很喜欢这个功能:)

On a lot of browsers I've tested, JavaScript blocks actually return a value. You can test it out in any console:

for(var i = 0; i < 10; i++) {
    var sqrt = Math.sqrt(i);
    if(Math.floor(sqrt) === sqrt) {
        i;
    }
}

The "return" value is the last square number, that is, 9! But since it isn't an expression I suppose, you can't do this:

for(var i = 0; i < 10; i++) {
    ...
} + 5

That doesn't work. It gives + 5, or 5, of course, because it's a separate statement. Putting the loop in parentheses obviously fails, and if a block is in parentheses (e.g. ({f(); r}) - doesn't work) it's treated as an object and throws a syntax error.

One way to take advantage of the return value, as such, is to use eval:

eval('for(var i = 0; i < 10; i++) {var sqrt = Math.sqrt(i);if(Math.floor(sqrt) === sqrt) {i;}}') + 5; // 14

But I'm obviously not going to want to use that if eval is the only solution. Is there a way to use a block's resultant value without using eval that I'm missing? I really like this feature :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忱杏 2024-12-29 16:24:34

在 JavaScript 中,语句返回 Completion 类型的值(这不是语言类型,而是规范类型)。

Completion类型用于解释语句的行为
breakContinuereturnthrow)执行非本地传输
控制。 Completion 类型的值是以下形式的三元组 (type,
目标),其中类型正常中断、<之一强>继续,返回
抛出是任何ECMAScript语言值或,以及目标
是任何 ECMAScript 标识符或

来源:http://es5.github.com/x8.html#x8.9

因此,eval() 评估作为源文本传入的程序。该程序(与任何 JavaScript 程序一样)返回一个 Completion 值。此完成值中的第二项(“值”项)由 eval() 调用返回。

因此,使用eval,您可以检索 JavaScript 程序的完成值。我不知道有任何其他方法可以实现此目的......

In JavaScript, statements return values of the Completion type (which is not a language type, but a specification type).

The Completion type is used to explain the behaviour of statements
(break, continue, return and throw) that perform nonlocal transfers of
control. Values of the Completion type are triples of the form (type,
value, target), where type is one of normal, break, continue, return,
or throw, value is any ECMAScript language value or empty, and target
is any ECMAScript identifier or empty.

Source: http://es5.github.com/x8.html#x8.9

So, eval() evaluates the program that has been passed in as source text. That program (like any JavaScript program) returns a Completion value. The second item in this Completion value (the "value" item) is returned by the eval() invocation.

So, with eval you are able to retrieve the completion value of an JavaScript program. I am not aware of any other method to accomplish this...

尘曦 2024-12-29 16:24:34

ES7 建议引入一个 do 表达式,它允许任何块被翻转变成一个表达式。 do 表达式计算块并返回其完成值。

使用此语法,您今天可以使用 Babel 尝试使用 syntax-do-expression transform-do-expression 插件,您的示例将看起来像这样:

function lastSquareNumber(val) {
    return do { for(var i = 0; i < val; i++) {
        var sqrt = Math.sqrt(i);
        if(Math.floor(sqrt) === sqrt) {
            i;
        }
    }}
}

console.log(lastSquareNumber(10));

There is a proposal for ES7 to introduce a do expression which allows any block to be turned into an expression. A do expression evaluates a block and returns its completion value.

Using this syntax, which you can try out today with Babel using the syntax-do-expression and transform-do-expression plugins, your example would look like this:

function lastSquareNumber(val) {
    return do { for(var i = 0; i < val; i++) {
        var sqrt = Math.sqrt(i);
        if(Math.floor(sqrt) === sqrt) {
            i;
        }
    }}
}

console.log(lastSquareNumber(10));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文