LoginSignup
25
26

More than 1 year has passed since last update.

最初の一回だけ実行される関数を生成する関数

Last updated at Posted at 2012-07-07

地味によく使う

var toExecutableOnce = function(f){
    var called = false, result = undefined;
    return function(){
        if(!called){
            result = f.apply(this, arguments);
            called = true;
        }   
        return result;
    };  
};
var greeting = toExecutableOnce(function(){
    console.log("hello");
    return "world";
});
console.log(greeting());// hello world
console.log(greeting());// world
25
26
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
26