Skip to content

Instantly share code, notes, and snippets.

View travishen's full-sized avatar
🕶️
JOMO

travishen.tw travishen

🕶️
JOMO
View GitHub Profile
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
// this case will cause automatic semicolon insertion
// and return wrong value when invoking this function
function getPerson() {
return
{
name: 'Tony',
}
}
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;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
// Function Statement
greet(); // hoisting
function greet(){
console.log('hi');
};
// FUNCTION EXPRESSION (ANONYMOUS)
anonymousGreet(); // undefined is not a function
// 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