Skip to content

Instantly share code, notes, and snippets.

View travishen's full-sized avatar
🎹
Play till the end.

ssivart travishen

🎹
Play till the end.
View GitHub Profile
var
// JavaScript is liberal about white space!
firstname,
// JavaScript is so liberal about white space!!
lastname,
// JavaScript is such liberal about white space!!!
// Oh my god!
email;
class BaseChanger:
BASE = 10
NEW_BASE = None
MINUS_SIGN = False
def __init__(self, n, new_base, n_base=BASE):
self.NEW_BASE = new_base
if n_base:
self.BASE = n_base
// Function Statement
greet(); // hoisting
function greet(){
console.log('hi');
};
// FUNCTION EXPRESSION (ANONYMOUS)
anonymousGreet(); // undefined is not a function
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
// Global variable
var greeting = 'Hola';
// Immediately Invoked Function Expressions
(function(global, name) {
var greeting = 'Hello';
consoel.log(global.greeting); // 'Hola'
console.log(greeting + ' ' + name); // 'Hello John'
function greet(whattosay) {
return function(name) {
consoel.log(whattosay + ' ' + name); // include variable whattosay by scope chain
}
}
greet('Hello')('John'); // 'Hello John'
@travishen
travishen / closures-example.js
Created September 16, 2018 09:59
Classic examples of JavaScript closure
function buildFunctions() {
var arr = [];
for(var i=0; i<3; i++) {
// add three function to arr
arr.push(
function() {
console.log(i);
@travishen
travishen / function-factory-with-closure.js
Created September 17, 2018 02:52
Create a factory using closure
function makeGreeting(language) {
return function(name){
if(language == 'en') console.log('Hello' + name);
if(language == 'es') console.log('Hola' + name);
}
}
var greetEnglish = makeGreeting('en');
greetEnglish('John'); // Hello John
@travishen
travishen / closure-example-settimeout.js
Last active September 17, 2018 03:06
An example that setTimeout using closure
function sayHiLater() {
var greeting = 'Hi';
// uses first-class function and create a function object on the flay by function expressions
setTimeout(function() {
console.log(greeting); // 30 sec later, it still have access to greeting variable
}, 30000);
}
@travishen
travishen / callback.js
Created September 17, 2018 05:35
Callback function example
function callLater(callback) {
console.log('Some work...');
callback();
}
callLater(function() {