简体   繁体   中英

How do I implement a before_filter callback in javascript?

Say I got this singleton object with some public methods and one private method:

var QuestionFactory = (function() {

// private method
function google_it_first() { ... }

// 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM