0

Is there any difference between creating a Task constructor with a var vs just a function?

function Task(name, priority, timeout){
this.name = name;
this.priority = priority;
this.timeout = timeout;
this.completed = false;
}
var task1 = new Task("firsttask",10,70s);

Versus

var Task = function(name, priority, timeout){
this.name = name;
this.priority = priority;
this.timeout = timeout;
this.completed = false;
}
var task1 = new Task("secondtask",20,30s);
serah
  • 1,551
  • 4
  • 22
  • 41
  • Read the answer here: http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname – arsho Jul 09 '16 at 18:27
  • So the answer given by joeytwiddle the correct answer? Read through the post and it is quite vague. It talks about how hoisting will throw an error in the first case while it wont in the latter. Is that the only difference? Would appreciate if I can get a more detailed explanation. I am quite new to Javascript and I am trying to understand the nuances. – serah Jul 09 '16 at 18:46

1 Answers1

0

// YOU CAN'T call this task before initialization. reason unset variable cant be call

var task1 = new Task("secondtask",20,30s); // This will raise error..
var Task = function(name, priority, timeout){
this.name = name;
this.priority = priority;
this.timeout = timeout;
this.completed = false; 
}
Muhammad Ali
  • 1,894
  • 1
  • 13
  • 19