Skip to content

Instantly share code, notes, and snippets.

@neraliu
Last active September 21, 2016 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neraliu/6a208da4cb4bb2a79e721e3e4f636b2c to your computer and use it in GitHub Desktop.
Save neraliu/6a208da4cb4bb2a79e721e3e4f636b2c to your computer and use it in GitHub Desktop.
running customized tasks with one interface run() in Javascript
(function() {
// object taskA
function taskA(name) {
this.name = name;
}
taskA.prototype.name = null;
taskA.prototype.run = function() {
console.log("taskA is running");
return true;
};
// object taskB
function taskB(name) {
this.name = name;
}
taskB.prototype.name = null;
taskB.prototype.run = function() {
console.log("taskB is running");
return true;
}
function runTask(task) {
if (task.run) {
return task.run();
} else {
return false;
}
}
function runTasks(tasks) {
var r = false;
if (!tasks instanceof Array) {
return r;
}
for(var i=0; i<tasks.length; ++i) {
if (tasks[i].run) {
r = tasks[i].run();
if (r === false) {
return false;
}
}
}
return true;
}
var A = new taskA("taskA");
var B = new taskB("taskB");
runTask(A);
runTask(B);
var tasks = [A, B];
runTasks(tasks);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment