Skip to content

Instantly share code, notes, and snippets.

View ivan-ha's full-sized avatar
👨‍💻
cannot read property 'status' of undefined 😄

Ivan Ha ivan-ha

👨‍💻
cannot read property 'status' of undefined 😄
View GitHub Profile
@ivan-ha
ivan-ha / closure-explanation.js
Last active February 22, 2017 16:55
js closure explanation
// same execution context, all pointing to same memory location
// so by the time running console.log(i), i will be its final value (i.e. 3)
function buildFunctions() {
var arr = [];
for (var i = 0; i < 3; i++) {
arr.push(
function () {
console.log(i);
}
);
@ivan-ha
ivan-ha / function-factory.js
Created February 23, 2017 13:05
Function factory using closure in js
function makeGreeting(language) {
return function (name) {
if (language == 'en') {
console.log('Hello ' + name);
}
else if (language == 'es') {
console.log('Hola ' + name);
}
};
}
@ivan-ha
ivan-ha / bookmark-url.md
Last active October 12, 2017 01:48
My bookmark url list. Updating from time to time
@ivan-ha
ivan-ha / call-apply-bind.js
Created March 1, 2017 15:31
Usage of call(), apply(), bind() in js
var person = {
firstname: 'John',
lastname: 'Doe',
getFullName: function() {
return this.firstname + ' ' + this.lastname;
}
}
var logName = function(arg1, arg2) {
console.log('Logged: ' + this.getFullName());
@ivan-ha
ivan-ha / functional-programming.js
Created March 2, 2017 14:04
Functional programming in js
function mapForEach(array, fn) {
var tmpArr = [];
for (var i = 0; i < array.length; i++) {
tmpArr.push(
fn(arr[i])
);
}
return tmpArr;
;(function(global, $) {
// 'new' an object
var Greetr = function(firstName, lastName, language) {
return new Greetr.init(firstName, lastName, language);
}
// hidden within the scope of the IIFE and never directly accessible
var supportedLangs = ['en', 'es'];
var fibonacci = function(number) {
var mem = {};
function f(n) {
var value;
if (n in mem) {
value = mem[n];
}
else {
/**
* Simulate classical ineritance (not recommended) using constructor function
*/
function Greeter (name) {
this.name = name || 'John Doe';
}
Greeter.prototype.hello = function hello () {
return 'Hello, my name is ' + this.name;
@ivan-ha
ivan-ha / greeter-concatenative.js
Created April 6, 2017 02:02
JS Concatenative Inheritance
const proto = {
hello: function hello() {
return `Hello, my name is ${ this.name }`;
}
};
const george = Object.assign({}, proto, {name: 'George'});
const msg = george.hello();
@ivan-ha
ivan-ha / map-filter-reduce.js
Last active June 26, 2017 03:36
Simple note on map, filter and reduce on JS
const animals = [
{
"name": "cat",
"size": "small",
"weight": 5
},
{
"name": "dog",
"size": "small",
"weight": 10