0

Possible Duplicate:
What is the difference between a function expression vs declaration in Javascript?

What is the difference in behavior between these two implementations? Is MyType still a constructor function in Implementation 1

Implementation 1:

var MyType = function() { 

}; 

var myObject = new MyType();

Implementation 2:

function MyType() { 

}

var myObject = new MyType();
Community
  • 1
  • 1
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • What's the purpose of the code inside these constructors? – Lèse majesté Jul 15 '12 at 17:01
  • 1
    @Lèse majesté I use the `init` idiom to encapsulate constructor-like functionality. Is there a better way? These types have been stripped of functionality for the purposes of the question here of course. I immediately create a `that` variable to ensure I get consistent `this` behavior. – Ben Aston Jul 15 '12 at 17:05
  • @Ben yes there are better ways (look into prototypes and function binding), they are also irrelevant to your question so I removed them to make it easier to quickly see the difference between the implementations. – Esailija Jul 15 '12 at 18:05

0 Answers0