构造函数 - javascript

时间:2016-07-09 18:24:40

标签: javascript

使用var与函数创建Task构造函数之间有什么区别吗?

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

对战

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);

1 个答案:

答案 0 :(得分:0)

//在初始化之前无法调用此任务。原因是未设置变量无法调用

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; 
}