LoginSignup
0
0

More than 3 years have passed since last update.

JavaScriptの関数

Last updated at Posted at 2020-09-29

コールバック関数

何らかの動作後に実行したい関数を実装する手段。
引数として関数を渡すことで、任意のタイミングで関数の処理実行を実現している。

test.js
function func(callback) {
 console.log('word1');
 callback();
}

func (function(){
 console.log('word2');
});

//実行結果は word1 word2と順に表示される。

巻き上げ

verを使用して関数内で変数を宣言したばあい、全てその関数の先頭で宣言されたものとみなされる。
これは、関数内のどこでも変数を宣言できる代わりに受ける制約。

test.js
ver a = 'word1';

function func() {
 console.log(a); //undefined
 ver a = 'word2';
 console.log(a); //word2
}

f();

実際の動作

test.js
ver a = 'word1';

function func() {
 ver a;
 console.log(a); //undefined
 a = 'word2';
 console.log(a); //word2
}

f();

即時関数

関数の定義と同時に実行する手段。
再利用しない場合などに使用できる。

test.js

(function(a, b) {console.log(a + b);}(1, 2)); //3
0
0
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
0
0