IEFE or iffy is a JavaScript function that, as the name says, is executed immediately. Here is the syntax. The key is the brackets at the start and end of function. This helps to keep the scope of the variables within the function. Without the function workerProcess and worker will have global scope which is BAD!!!
(function () {
var workerProcess = function () {
var task1 = function () {
console.log("task 1");
};
var task2 = function () {
console.log("task 2");
};
return {
job1: task1,
job2: task2
};
};
var worker = workerProcess();
worker.job1();
worker.job2();
worker.job2();
}());
(function () {
var workerProcess = function () {
var task1 = function () {
console.log("task 1");
};
var task2 = function () {
console.log("task 2");
};
return {
job1: task1,
job2: task2
};
};
var worker = workerProcess();
worker.job1();
worker.job2();
worker.job2();
}());
Comments