Skip to content

Instantly share code, notes, and snippets.

@juan-m-medina
Last active January 21, 2016 19:06
Show Gist options
  • Save juan-m-medina/c560d378561fdfccd525 to your computer and use it in GitHub Desktop.
Save juan-m-medina/c560d378561fdfccd525 to your computer and use it in GitHub Desktop.
"use strict";
/*
///------------------------------------------------------------------------------------
/// Hoisting example
(function badHoistTest() {
console.log(prettyPrint(a)); // 'a' is known, not undefined, declaration is hoisted
var a = 'ugly'; // Initializations are NOT hoisted, only declarations;
function prettyPrint(input) {
return `Bad hoisting shows undefined: *** ${input} ***`;
}
})();
(function goodHoistTest() {
var a = 'ugly';
console.log(prettyPrint(a));
function prettyPrint(input) {
return `Good hoisting shows the variable value *** ${input} ***`;
}
})();
*/
/*
///------------------------------------------------------------------------------------
// Functions as variables
var testFunction = function() {
return "I am a function";
};
console.log(testFunction);
console.log(testFunction());
*/
/*
///------------------------------------------------------------------------------------
/// IIFE (Immediately-Invoked Function Expression) all over!
(function() {}
)();
(function() {
console.log('normal!');
})();
console.log((function() {
return 'returned!';
})());
(function badFunction() {
console.log('I mess things up');
})() /// !!!! Missing semi colon!!!
(function noWorky() {
console.log("I've been messed up and you won't find out");
})();
*/
/*
///------------------------------------------------------------------------------------
/// Scoping
(function scope() {
var inScopeOne = 1;
for (var i=1; i < 5; i++) {
var blah = i;
}
console.log("Blah is not scoped to the brackets, has value: " + blah);
function innerFunction() {
var unreachableOutsideFunction = 100;
console.log("Inside inner open function, reachableOutsideFunction value is: " + unreachableOutsideFunction);
};
innerFunction();
console.log("Outside inner open function, reachableOutsideFunction value is: " + unreachableOutsideFunction);
})();
*/
///------------------------------------------------------------------------------------
/// Closure
/*
(function outer() {
var a = 10;
setTimeout(function inner() {
console.log(a);
}, 3000);
console.log('Exiting parent');
})();
*/
/*
///------------------------------------------------------------------------------------
/// What "this" means
console.log(this); // At this level, undefined
function blah() {
console.log(`Inside Regular function: ${this}`); // At this level, no strict, the parent is window
};
blah();
(function iife() {
console.log(`Inside IIFE: ${this}`); // At this level, no strict, the parent is window
})();
(function iifeWithChild() {
function child() {
console.log(`Inside ChildFunction: ${this}`); // At this level, no strict, the parent is window
}
child();
})();
/// How strict affects Object not being strictly declared
(function strictIifeWithChild() {
"use strict";
function child() {
console.log(`Inside Strict ChildFunction: ${this}`); // At this level, with strict not object is defined, so undefined this
}
child();
})();
/// How strict affects Object being strictly declared
var testObject = {
objectName : "testObject",
testFunction: function testFunction() {
"use strict";
console.log(`I am inside the testFunction and this is: ${this.objectName}`);
}
};
testObject.testFunction();
*/
/*
///------------------------------------------------------------------------------------
/// Closure showing issues with this
var callableObject = {
objectName : "I am the callable Object",
callableFunction : function(){
console.log(`Inside callback, this is: ${this.objectName}`);
}
};
var callerObject = {
objectName : "I am the caller Object",
closureTestFunction : function() {
console.log(`Inside closureTest dubb-dubb Function, this is: ${this.objectName}`);
callableObject.callableFunction();
}
}
callerObject.closureTestFunction();
*/
/*
/// Old School inheritance
var baseObject = {
name : 'Rick',
travel : function(subject) {
return `Go far my friend ${subject}`;
},
function() {
console.log('Perhaps unintended call');
}
};
var instanceOfBase = Object.create(baseObject);
console.log(`${instanceOfBase.name}`);
console.log(instanceOfBase.travel('Juan'));
var baseFunction = function(name) {
console.log(name);
};
var instanceOfBaseFunction = new baseFunction('Joe');
*/
/*
///------------------------------------------------------------------------------------
/// ECMA 6 class and prototypal inheritance
class SaluteClass {
constructor(firstName, lastName) {
console.log(firstName);
this.firstName = firstName;
this.lastName = lastName;
}
salute() {
console.log(`Hello ${this.firstName} ${this.lastName}`);
}
}
class InteractionClass extends SaluteClass {
goodbye() {
console.log(`Goodbye ${this.firstName} ${this.lastName}`);
}
}
let chat = new InteractionClass('Rick', 'Sanchez');
chat.salute();
chat.goodbye();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment