1 - example of a closure .
// example 1 //one has an arg and two also has one
function one(x) {
function two(y) {
console.log( x + y );
}
return two;
} //one
//first
var first = one(5);
//second
var second = first(10); //remembers the five from before or the whatever the x value
//returns 15
// example 2 closure returning object
function person(name) { //only name arg is passed
function say() { //no arg here
console.log('hello ' + name);
}//say
return {
greeting:say
};
}//person
var mark = person('Mark');
mark.greeting();
//returns hello Mark
// example 3 //person no arg and obj notation to grab first and last
function person() {
function firstName(first) {
console.log('first name is: ' + first);
}//say
function lastName(last) {
console.log('last name is: ' + last);
}
return {
firstName: firstName,
lastName: lastName
};
}//person
var guyOne = person();
//firstname
guyOne.firstName('John'); //returns first name is John
//lastname
guyOne.lastName('Morris'); //return last name is Morris