Say I got this singleton object with some public methods and one private method:
var QuestionFactory = (function() {
// private method
function google_it_fir开发者_Go百科st() { ... }
// public methods
return {
ask_a_stupid_question: function() { ... },
ask_a_relatively_non_stupid_question: function() { ... },
ask_a_difficult_question: function() { ... }
}
})();
What I'd like to be able to do is call google_it_first
method first, when any public method is called. How do I implement it without explicitly calling it inside each public method?
function wrapper() {
return function() {
google_it_first();
return arguments[0](arguments.slice(1,arguments.length);
}
}
ask_a_stupid_question: wrapper(function() { ... })
Of the top of my head, I think that would work.
精彩评论