Skip to content

Instantly share code, notes, and snippets.

$(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
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
function firstFunc(){
var firstVar = 2;
firstVar += secondVar; // secondVar is undefined because scope doesn’t look down
function secondFunc(){
var secondVar = 5;
}
}
@rayinla
rayinla / bind-me.js
Last active January 18, 2018 16:26
$(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)
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);
// 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
}
}
function init(args){
var firstName = args.firstName;
var lastName = args.lastName;
function gerunding(action){
return firstName + " " + lastName + " " + "is" + " " + action;
}
return gerunding;
}
var zombieOne = (function(){
//private variables
var firstName = "";
var lastName = "";
//private functions
function init(data){
firstName = data.firstName;
lastName = data.lastName;
}
/*
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
//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