Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
memoize = function(func, context, single) {
function memoizeArg (argPos, depth) {
var cache = {};
return function () {
if (argPos == 0 && depth == 0) {
argPos = arguments.length;
}
if (argPos <= 0) {
if (!(arguments[argPos] in cache)) {
cache[arguments[argPos]] = func.apply(context, arguments);
}
return cache[arguments[argPos]];
}
else {
if (!(arguments[argPos] in cache)) {
if (single) {
cache = {};
}
cache[arguments[argPos]] = memoizeArg(argPos - 1, depth + 1);
}
return cache[arguments[argPos]].apply(this, arguments);
}
}
}
return memoizeArg(func.length - 1, 0);
}
итого..
var calculationFunctor = memoize(calculation);
don't use <source>, Luke
Daddy.prototype = {
constructor: Daddy,
//...
}, то тут недавно спрашивали: habrahabr.ru/blogs/javascript/132340/#comment_4393309Daddy.prototype = {typeof, а instanceof!var Man = function() {
this.hasBalls = true;
}
Man.prototype = {
say: function() {
console.log('Yeah!');
}
};
var Dad = function() {
// как автоматом проставить, чтоб у него были яйца?
// как получить доступ к методу say или переопределить его?
// только передав Man в качестве параметра?
}
function calculation(x, y){
var key = x.toString() + y.toString();
<code class="javascript">
function decorator(fn) {
function decoratorInner(arg) {
console.log('decorator');
return fn(arg);
}
return decoratorInner;
};
function test(arg) {
console.log('test ' + arg);
return 'test result';
};
console.log(test('#test arg'));
var decoratedTest = decorator(test);
console.log(decoratedTest('#decorated test arg'));
</code>
JavaScript паттерны… для чайников