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,922
  • 2
  • 44
  • 52

1 Answers1

0

The difference come here:

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

console.log(typeof bar); //'undefined'
var bar = function () {
}
Minko Gechev
  • 25,304
  • 9
  • 61
  • 68