Skip to content

fix methods properties #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

fix methods properties #2

wants to merge 1 commit into from

Conversation

apsavin
Copy link

@apsavin apsavin commented Jan 21, 2013

Sometimes it is very useful to set a flag to function - for example, for auto-wrap it in $.memoize in constructor.

@apsavin
Copy link
Author

apsavin commented Feb 1, 2013

Anybody?..

@dfilatov
Copy link
Owner

dfilatov commented Feb 2, 2013

А можешь привести конкретный пример использования этого вместе с $.memoize?

@apsavin
Copy link
Author

apsavin commented Feb 2, 2013

Пример ниже
pseudo code:

class A {
    doSmth: $.memoize(function (a) { return this.b + a; });
}

Может вызвать ошибку, так как функция находится в прототипе и запоминаться ответы будут для всех инстансов. На всякий случай - jsfiddle. Можно избежать проблемы, обернув в $.memoize в конструкторе. Чтобы с этим не замарачиваться, удобно ставить функции флаг, например, так:

$._memoize = function (fn) {fn.memoizable = true; return fn;};
$._memoize(function (a) { return this.b + a;});

В конструкторе проходить по методам, смотреть у них наличие флага и, например, оборачивать в $.memoize. В наследниках класса, соответственно, дублировать не придется, можно будет любые функции помечать флагами.

Это только один из примеров. Вообще - можно придумать кучу разных примеров, для чего в свойства метода может понадобиться что-то сохранить.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants