这是JavaScript上的return关键字的预期行为

tug*_*erk 3 javascript

今天我濒临愚蠢的错误,把我所有的头发拉到了一边.问题是我不确定这是否是预期的行为.

根据我对C#的习惯,我编写了以下javascript代码,并试图了解出现了什么问题,因为它没有返回任何内容,甚至没有在控制台中写入任何错误:

this.fullName = ko.computed(function () {

    return
        this.firstName() + " " + this.lastName();

}, this);
Run Code Online (Sandbox Code Playgroud)

然后(半小时后),我更改了下面的代码并且它有效:

this.fullName = ko.computed(function () {

    return this.firstName() + " " + this.lastName();

}, this);
Run Code Online (Sandbox Code Playgroud)

这是预期的行为吗?

Ser*_*sev 10

是.这是隐含的分号.

以下是与解释类似的情况:http://premii.com/lab/js_implicit_semicolon_and_return.php

简而言之:您的第一个片段被解释为:

this.fullName = ko.computed(function () {
    return;
    this.firstName() + " " + this.lastName();
}, this);
Run Code Online (Sandbox Code Playgroud)