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

Differences between types of Javascript function declarations - Stack Overflow

programmeradmin2浏览0评论

While reading about IronJS, I can across the article here /

In it is the following:

*Context sensitive function keyword

In case you didn’t know, these two functions are not identical:

(function bar() { })  

function foo() { } 

Finding out the difference I’ll leave as an exercise to the reader.

Can some explain the difference here?

While reading about IronJS, I can across the article here http://ironjs.wordpress.com/

In it is the following:

*Context sensitive function keyword

In case you didn’t know, these two functions are not identical:

(function bar() { })  

function foo() { } 

Finding out the difference I’ll leave as an exercise to the reader.

Can some explain the difference here?

Share Improve this question edited Jun 22, 2011 at 20:44 Brad 163k55 gold badges377 silver badges552 bronze badges asked Jun 22, 2011 at 20:30 GazGaz 3,9156 gold badges29 silver badges33 bronze badges 4
  • 3 One is a function declaration and the other is a named function expression. Finding out which is which I'll leave as an exercise to the reader. ;) – Felix Kling Commented Jun 22, 2011 at 20:39
  • 3 If the reader reads kangax on NFE (named function expressions) they should be enlightened. kangax.github.com/nfe – Sean Vieira Commented Jun 22, 2011 at 20:41
  • @SeanVieira Thanks for the link, look like a good article! – Gaz Commented Jun 22, 2011 at 20:49
  • related: Explain JavaScript's anonymous function syntax and var functionName = function() {} vs function functionName() {} – Bergi Commented Jul 16, 2014 at 21:40
Add a comment  | 

4 Answers 4

Reset to default 9
function foo() { }

Creates a function

(function foo(){ })

Returns a function object. You can also use:

(function foo(){ })(bar)

And make an anonymous function. (Note that the (bar) means that within that function this refers to the bar instance.)

Check out this other SO post for more information.

I am guessing the difference is that the first one is not visible to the global scope and the latter is visible globally.

To expand on @Amir's answer:

js>(function bar() {})(3)
js>bar
console:1       ReferenceError: bar is not defined
js>function foo() {}
js>foo
function foo() {
}

(code executed in jsdb)

These are named functions, and if you don't put parentheses around the function definition, they become part of the local scope. function foo() {} becomes available for use later, but bar does not.

As a third example:

var x = function baz() {};

If you run this:

js>var x = function baz() {}
js>baz
console:1       ReferenceError: baz is not defined

You'll note that it's the similar case as (function baz(){})(3).

The case of

function foo() {}

is special, the Javascript interpreter sees that form and says, "Oh, you're trying to define a function named "foo" in the local scope."

As for why a named function is useful even if it doesn't get defined in the local scope -- the named function is visible from the scope of the function itself:

js>var x = function fact(n) { return n*((n < 2) ? 1 : fact(n-1)); }
js>x(3)
6
js>fact
console:1       ReferenceError: fact is not defined

Here we have a factorial function named "fact", but the name "fact" is only visible inside the scope of the function itself.

The first function is a named anonymous function (yeah). The expression evaluates to a Function. The second one defines a named function and returns undefined.

发布评论

评论列表(0)

  1. 暂无评论