Skip to content

Instantly share code, notes, and snippets.

View travishen's full-sized avatar
🕶️
JOMO

travishen.tw travishen

🕶️
JOMO
View GitHub Profile
@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() {
@travishen
travishen / js-function-this-control.js
Created September 17, 2018 08:20
Use function method: bind, call, apply to change this variable in function
var person = {
firstname: 'John',
lastname: 'Doe',
getFullName: function() {
var fullname = this.firstname + ' ' + this.lastname;
return fullname;
}
}
@travishen
travishen / js-functional-programming.js
Created September 17, 2018 09:15
Functional programming using JavaScript
/* No wraping */
var arr1 = [1, 2, 3];
var arr2 = [];
var limiter = 2;
arr1.forEach(function(item, i){
arr2.push(item > 2);
});
console.log(arr2); // [false, false, true]
@travishen
travishen / undefined.js
Created September 18, 2018 06:17
JavaScript type undefined
a === undefined; // Uncaught ReferenceError: a is not defined
var a;
a === undefined; // true
@travishen
travishen / js-prototype-chain-reflection-extend.js
Last active September 19, 2018 11:05
Prototype chain, object reflection and extend
var person = {
firstname: 'Default',
lastname: 'Default',
getFullName: function(){
return this.firstname + ' ' + this.lastname;
}
}
var john = {
firstname: 'John',
@travishen
travishen / __proto__.js
Created September 18, 2018 08:51
Access prototype by __proto__ parameter
var arr = [];
var obj = {};
console.log(arr.__proto__.constructor); // ƒ Array() { [native code] }
console.log(object.__proto__.constructor); // ƒ Object() { [native code] }
console.log(arr.__proto__.__proto__.constructor); // ƒ Object() { [native code] }
console.log(object.__proto__.__proto__); // null
@travishen
travishen / js-new.js
Last active September 25, 2018 11:28
JavaScript operator new to construct an object via function
function Person(firstname, lastname) {
console.log('Invoke function: Person');
this.firstname = firstname || 'Default';
this.lastname = lastname || 'Default';
}
// JavaScript has no classes, use this syntax atract Java users
var john = new Person('John', 'Doe');
console.log(john);
// Invoke function: Person
function loadScript(scriptName, callback) {
if (!jsArray[scriptName]) {
var promise = jQuery.Deferred();
// adding the script tag to the head as suggested before
var body = document.getElementsByTagName('body')[0],
script = document.createElement('script');
script.type = 'text/javascript';
script.src = scriptName;
@travishen
travishen / set-proto-via-function-constructor.js
Created September 25, 2018 10:57
Set object prototype via function constructor
function Person(firstname, lastname) {
this.firstname = firstname || 'Default';
this.lastname = lastname || 'Default';
}
var john = new Person('John', 'Doe');
console.log(john.__proto__.constructor === Person); // true
// .prototype
Person.prototype.getFullName = function() {