77
87

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScript コールバックの作り方

Last updated at Posted at 2015-05-14

ぐぐっても簡単な説明がなかったのでメモです
関数の引数として関数を渡して実装

バナナは
黄色

callback.js
;(function(){

    // 
    var sayYellow = function(){
       console.log('黄色');
    };

    
    var sayBanana = function(aCallback){
        console.log('バナナは');
        aCallback();
    };

    // sayYellow関数を引数で渡す
    sayBanana(sayYellow);

    // 無名関数を書いて渡すのもあり
    sayBanana(function(){
        console.log('黄色');
    });

    // バナナは
    // 黄色
    
    // と出力される

})();

animate()や
setTimeout()や
addEventListener()
でいつも何となく使ってるけど
必要になって自分で作るとどうなんやと思いました

77
87
0

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
77
87

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?