Help us understand the problem. What is going on with this article?

関数宣言と関数式の違い

More than 5 years have passed since last update.

所謂、

//関数宣言
function hoge {}
//関数式
var hoge = function{}

の違い。
「関数と、変数にいれる関数ってなにが違うの?」という問の解。

関数の巻き上げがあるかどうかの違い

関数宣言の場合、関数の巻き上げにより
関数宣言前でも実行ができるが、
関数式の場合は変数のみが巻き上げられるため関数式以降でないと実行できない。

function someFunc(){
    foo(); // -> foo :関数宣言前でも実行可
    bar(); // -> TypeError: undefined is not a function :実行不可   

    function foo(){
        console.log('foo');
    }

    var bar = function(){
        console.log('bar');
    };

    bar(); // -> bar :関数式後なので実行可
}

someFunc();

巻き上げとは

関数内のどの場所で変数を定義しても、それらはその関数の先頭で宣言されたのと同じように動作すること。
関数の場合は宣言だけでなくその定義も巻き上げられる。

orangemittoo
ハマった事や備忘録を書く。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした