LoginSignup
2
3

More than 5 years have passed since last update.

Javascript関数の書き方いろいろ

Posted at

通常表記

// どこからでも呼べる
foo(); // bar
function foo(args){
    console.log('bar');
}

変数に関数を割り当て

var foo = function(args){
    console.log('bar');
};
foo(); // bar
foo.bar(); // undefine

foo.bar = function(args){
    console.log('foo bar');
};
foo(); // bar
foo.bar(); // foo bar
var foo = {
    bar : function(args){
            console.log('foo bar');
        },
    hoge: function(args){
            console.log('foo hoge');
        }
};
foo(); //TypeError: object is not a function
foo.bar(); // foo bar
foo.hoge(); // foo hoge
2
3
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
2
3