最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is this an expected behavior with return keyword on JavaScript - Stack Overflow

programmeradmin1浏览0评论

I was on the verge of pulling all of my hair today over a silly mistake. The thing is that I am not sure if this is an expected behavior.

With my habits on C#, I have written the following javascript code and tried to understand what is going wrong because it didn't return anything and even didn't write any error into the console:

this.fullName = koputed(function () {

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

}, this);

Then (after half an hour), I changed the code a below and it worked:

this.fullName = koputed(function () {

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

}, this);

Is this an expected behavior?

I was on the verge of pulling all of my hair today over a silly mistake. The thing is that I am not sure if this is an expected behavior.

With my habits on C#, I have written the following javascript code and tried to understand what is going wrong because it didn't return anything and even didn't write any error into the console:

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

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

}, this);

Then (after half an hour), I changed the code a below and it worked:

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

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

}, this);

Is this an expected behavior?

Share Improve this question asked Mar 17, 2012 at 12:51 tugberktugberk 58.5k69 gold badges251 silver badges342 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 10

Yes. It's the implicit semicolon.

Here's similar situation with explanations: http://premii./lab/js_implicit_semicolon_and_return.php

In short: your first snippet is interpreted as:

this.fullName = ko.puted(function () {
    return;
    this.firstName() + " " + this.lastName();
}, this);

Yes ECMA Spec, which is the Javascript spec, defines the behavior like this

Certain ECMAScript statements (empty statement, ... return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

Further more

When a continue, break, return, or throw token is encountered and a LineTerminator is encountered before the next token, a semicolon is automatically inserted after the continue, break, return, or throw token.

An Expression in a return or throw statement should start on the same line as the return or throw token.

And they have given an example of:

The source

return
a + b

is transformed by automatic semicolon insertion into the following:

return;
a + b;

So your first code will be interpreted as:

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

with the automatically added semi colon at the end of return.


So the spec gives practical advice to face these situations in general javascript:

A postfix ++ or -- operator should appear on the same line as its operand.

An Expression in a return or throw statement should start on the same line as the return or throw token.

A Identifier in a break or continue statement should be on the same line as the break or continue token.

Yes, it is. Javascript doesn't need a semicolon at the end of the line to end the statement. In this case, it simply returns.

Yes it is, Semicolons in javascript tend to be optional, So there is no exact way to differentiate the plete statement only using semicolons. Check out this book which I hear is a great resource for JS , The good parts of javascript by douglas crockford.

Javascript has a feather which is Automatic semicolon insertion, which is not good, you should not depend on it and add semicolon when a statement finishes.

In you case, javascript add ; to the return statement. so it is same as:

return;
this.firstName() + " " + this.lastName();
发布评论

评论列表(0)

  1. 暂无评论