Last active
September 21, 2016 05:05
-
-
Save neraliu/6a208da4cb4bb2a79e721e3e4f636b2c to your computer and use it in GitHub Desktop.
running customized tasks with one interface run() in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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