0

I am having a hard time understanding what the difference between the following declarations is...

var User = function(name, email) {
  this.name = name;
  this.email = email;
};

and

function User(name, email) {
  this.name = name;
  this.email = email;
};
user3289157
  • 645
  • 2
  • 13
  • 24

1 Answers1

1

What I know the very basic difference is var User = function(name, email) is created at runtime, and function User(name, email) will be created at parse time.

adding example

This will work here because name is available when it parsed the file

name();    
function name () {
    console.log('My Name')
}

But this will throw an error called undefined is not a function

But here problem was it won't be declared until it read the line var name = function().

name()
var name = function(){
    console.log('My Name');
}
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • 2
    "Parsing" is usally the process where some data is converted to another representation. So e.g. with JS it could be from text to some kind of byte code (I don't actually know). But at that moment it's impossible to create any runtime objects (like a function). The difference is that function declarations are evaluated before anything else. – Felix Kling Jul 26 '14 at 04:57