0

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

How are Foo and Bar any different ?

If objects were only functions, why was this new syntax introduced ? (Foo).

var Foo = function(arg) {
    this.attr = arg;
};

function Bar (arg)  {
    this.attr = arg;
}


/*
>>> f = new Foo(3)
Object { attr=3}
>>> f.attr
3
>>> b = new Bar(40)
Bar { attr=40}
>>> b.attr
40
*/

A fair amount of documentation I'v read proposes the first syntaxe, but the second one seems to work just as well.

Community
  • 1
  • 1
ychaouche
  • 4,242
  • 2
  • 40
  • 45

1 Answers1

-1

The difference come here:

console.log(typeof foo); //'function'
function foo() {
}

console.log(typeof bar); //'undefined'
var bar = function () {
}
Minko Gechev
  • 23,174
  • 7
  • 57
  • 66