标签云

微信群

扫码加入我们

WeChat QR Code

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}

There are two possible methods for pulling out a function in Javascript:

var foo = function() { ... }

(this is a bit contrived; another common pattern is:

var foo = {
    baz: 43,
    doSomething:function() {
        ...
    }
}

)

versus

function foo() { ... }

Is there an explicit reason to prefer one or the other?


Does the scoping of the first differ from the second?

2018年08月16日23分36秒

This is a nice article kangax.github.com/nfe

2018年08月16日23分36秒

amnotiam: Agreed. Voted to close.

2018年08月16日23分36秒

Just remember that function declarations in blocks is technically invalid, so it might be preferred to use function expressions then.

2018年08月16日23分36秒

Wow, it's so obvious that "function declarations" should not exist/be treated specially in javascript - they should just be an expression, like "named function expression", whose return value is ignored.

2018年08月16日23分36秒

JesseGood—ah yes, I mean inside if blocks. I'll fix that.

2018年08月16日23分36秒

JesseGood—function declarations inside blocks aren't "technically invalid" at all, if they were they'd throw a syntax error. There is no good reason to prefer function expressions over declarations where the two can be interchanged (noting that there are cases where function expressions are allowed but declarations aren't).

2018年08月16日23分36秒

RobG: Syntax errors are not the only issue. The difference can cause semantically different programs.

2018年08月16日23分36秒

The var foo = {... example is not JSON syntax. It's object literal syntax. JSON's data structure notations are based on JavaScript's object and array literal structures, but they're not the same thing.

2018年08月16日23分36秒

noted, I've tried to change the references to JSON properly. Do you have an example of JSON that isn't valid literal syntax , or vise-versa?

2018年08月16日23分36秒

Generally, if you're defining an object literal in JavaScript code, we don't refer to it as JSON, since the rest of the JS code makes it invalid JSON. Conversely, if we remove all the JS, and just have a standalone JSON structure like {"foo":"bar"} (since JSON keys must be double quoted), this doesn't constitute a valid JavaScript program. The result will be a SyntaxError. Also, there are some invisible characters that are allowed in JavaScript strings, but not JSON. Overall, the only connection between JS and JSON are the first two letters, and the general syntax of data structures.

2018年08月16日23分36秒

Here's a short article that gives some detail. Perhaps not entirely analogous, but I think of the relationship between JSON markup and JS language (or whatever language) as being similar to the relationship between HTML markup and a DOM. One is markup that can be parsed and rendered into the other.

2018年08月16日23分36秒

rtpg, you explained the difference well, and you made a very strong recommendation of using function expressions. However your reasons for doing are:"there's no reason not to". Someone who has a an opposing opinion would not be convinced. Only someone who already agrees with you will agree. My suggestion is to show several examples of what would be bad if someone were to using function declaration.

2018年08月16日23分36秒

What version(s) of IE? 9?

2018年08月16日23分36秒

IE<9. Older JScript engines, also Saf 2.1, Old opera, old firefox. Read the NFE article

2018年08月15日23分36秒