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
$(document).ready(function(){ | |
//It is important to notice that 'button' is a DOM object with various methods | |
$('button').on('click', function(){ | |
//We must preserve the button's context | |
var that = this; | |
bluezy(); | |
function bluezy(){ | |
//We're saying, "I want to do fun things with <<that>> button object, | |
//not <<this>> window object you keep giving me |
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
var myHouse = { | |
neighborhood: "Cherry Wood", | |
address: "5 Premium Park", | |
city: "Rolly", | |
state: "NC", | |
printInfo(){ | |
// 'this' refers to the object we created. | |
//'this' is easy to conceptualize here because... | |
//we have manually created an object that we can see | |
return this.neighborhood + "," + this.address + "," + this.city + "," + this.state |
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 firstFunc(){ | |
var firstVar = 2; | |
firstVar += secondVar; // secondVar is undefined because scope doesn’t look down | |
function secondFunc(){ | |
var secondVar = 5; | |
} | |
} |
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
$(document).ready(function(){ | |
//It is important to notice that 'button' is a DOM object with various methods | |
$('button').on('click', function(){ | |
//We must preserve the button's context | |
bluezy(); | |
function bluezy(){ | |
//We're saying, "I want to do fun things with <<that>> button object, | |
//not <<this>> window object you keep giving me | |
$(this).css("background-color", "blue"); | |
}.bind(this) |
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 sayMyName(firstName, middleName, lastName){ | |
var praiseList = ["beautiful", "awesome", "splendid", "fantastic", "jaw-dropping"]; | |
//this first closure called combineName() has access to the parameters | |
//of the outer function and its variable | |
function combineName(){ | |
var min = 0; | |
var max = praiseList.length - 1; | |
//this second getRandomInt() closure has access to the variables in combineName() | |
function getRandomInt(){ | |
return Math.floor(Math.random() * (max - min) + min); |
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
// We have two functions. One is named outie and the other is named closure *wink* *wink* | |
//this is closure's global scope | |
function outie(){ | |
// this is closure's first and only outer scope | |
function closure(){ | |
// this is closure's local scope | |
} | |
} |
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 init(args){ | |
var firstName = args.firstName; | |
var lastName = args.lastName; | |
function gerunding(action){ | |
return firstName + " " + lastName + " " + "is" + " " + action; | |
} | |
return gerunding; | |
} | |
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
var zombieOne = (function(){ | |
//private variables | |
var firstName = ""; | |
var lastName = ""; | |
//private functions | |
function init(data){ | |
firstName = data.firstName; | |
lastName = data.lastName; | |
} | |
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
/* | |
Closures cannot access the arguments object of the parent, | |
but, because functions are first class objects, we can pass a function as a parameter. | |
The closure can now access the arguments object of the function that is passesd as a parameter. | |
So, there is no confusion as to which arguments object we want the closure to access. | |
We're basically taking advantage of its limitations | |
*/ | |
function demoMemo(func){ | |
//we must return a function in order to keep state | |
//this will be more apparant in a recursive example |
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
//Knowing that we have access to whatever | |
//the user inputs into our function expression, we then write... | |
return function(){ | |
var key = JSON.stringify(arguments); | |
if (cash[key]){ | |
return cache[key]; | |
} | |
else{ | |
//apply() comes in handy here and will simply |
OlderNewer