开发者

How do I implement a before_filter callback in javascript?

开发者 https://www.devze.com 2023-01-03 21:18 出处:网络
Say I got this singleton object with some public methods and one private method: var QuestionFactory = (function() {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消