Created
June 29, 2020 08:10
-
-
Save JoseJPR/b8465a46798004956b43e669724ef32a to your computer and use it in GitHub Desktop.
JavaScript Closures with Functions as Parameters. In this example you can see that work with closure. You can define a function and execute when pass the rules of the other function. In this example you can create a vehicle if the road previously exist.
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
/** | |
* Title: JavaScript Closures with Function as Parameter. | |
* | |
* Description: In this example you can see that work with closure. You can define | |
* a function and execute when pass the rules of the other function. | |
* In this example you can create a vehicle if the road previously exist. | |
* | |
* More Info: https://developer.mozilla.org/es/docs/Web/JavaScript/Closures | |
*/ | |
// Define Anonymous Function for encapsulate from Root Scope. | |
(function () { | |
// Create Main Closure Function for Road Element. | |
function road() { | |
// Set default properties and values. | |
let status = 'not exist'; | |
// Contructor for return all instance. | |
function constructor () { | |
return this; | |
} | |
// Function for check if can execute "func" param or not. If can execute it, a vehicle will be buy. | |
this.control = function (func) { | |
if (status === 'exist') return func; else throw Error('The road not exist and is required.'); | |
} | |
// Toggle and Get Status Method. | |
this.toggleStatus = function () { | |
return status === 'not exist' ? status = 'exist' : status = 'not exist'; | |
} | |
this.getStatus = function () { | |
return status; | |
} | |
return constructor(); | |
} | |
// Create Main Closure Function for Vehicle Element. | |
function vehicle(road) { | |
// Set default properties and values. | |
let status = 'not buyed'; | |
// Contructor for return all instance. | |
function constructor () { | |
// When constructor is executed the vehicle status change to "buyed". | |
this.toggleStatus(); | |
return this; | |
} | |
// Toggle and Get Status Door Methods. | |
this.toggleStatus = function () { | |
return status === 'not buyed' ? status = 'buyed' : status = 'not buyed'; | |
} | |
this.getStatus = function () { | |
return status; | |
} | |
// Control for that you can buy a vehicle only if the road exist. | |
return road.control(constructor); | |
} | |
// Create Road Element. | |
const roadA = road(); | |
/** | |
* If you comment the follow line the road not exist, then the vehicle not can be created. | |
*/ | |
roadA.toggleStatus(); | |
try { | |
// Show if exist or not the road. | |
console.log('Exist the Road?:', roadA.getStatus()); | |
// Define Vehicle (A) with Vehicle Object. | |
const vehicleA = vehicle(roadA)(); | |
// Get Status Vehicle when exist a road. | |
console.log('Get status Vehicle:', vehicleA.getStatus()); | |
// Toggle and Get Status Vehicle method. | |
console.log('Change status Vehicle:',vehicleA.toggleStatus()); | |
console.log('Get status Vehicle:', vehicleA.getStatus()); | |
} catch (err) { | |
console.log(err.message); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment