const memoize = fn => {
let cache = {};
return (...args) => {
let stringifiedArgs = JSON.stringify(args);
let result = cache[stringifiedArgs] = cache[stringifiedArgs] || fn(...args);
return result;
};
};
const getItemsMemoised = memoize((x, y) => x+y);
//const getItems = memoize((x) => x+2);
console.log(getItemsMemoised(2, 3));
const UserSchema = {
name: null,
salt: null,
makeSalt(){
this.salt = Math.round((new Date().valueOf() * Math.random())) + '';
}
};
let user = UserSchema;
user.name = 'Paul';
user.makeSalt();
console.log(user);
function app(arg){
const main = (() => { return arg + 2; } )();
return main;
}
let x = app(2);
console.log(x);
const data = [
{ step1: false },
{ step2: false },
{ step3: false },
{ step4: false }
];
function stepHandler(name) {
const fixObj = elem => {
if (elem[name] !== undefined) {
return Object.assign({}, {[name]: true});
}else return elem;
};
const newData = data.map(fixObj);
console.log(newData);
}
stepHandler("step1");
const g = n => n + 1;
const f = n => n * 2;
const h = x => f(g(x));
h(20);
console.log(h(20));
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
let user = {
firstName: "John",
sayHi: function(phrase) {
console.log(`${phrase}, ${this.firstName}!`);
}
};
let say = user.sayHi.bind(user);
setTimeout(say, 1000, 'Hello'); // Hello, undefined!