LoginSignup
0
0

More than 5 years have passed since last update.

[クイズ]javascriptと仲良くなるための一歩 第43話「this参照2」

Posted at

問題

var obj = {
           x:3,
           doit: function(){ console.log("method is called." + this.x); }
          }
obj.doit(); //=> method is called.3

var fn = obj.doit;
fn(); //=> ?

:mouse:
:cow:
:tiger:
:rabbit:
:dragon_face:
:snake:
:horse:
:sheep:
:monkey_face:
:bird:
:dog:
:boar:
:mouse:
:cow:
:tiger:
:rabbit:
:dragon_face:
:snake:
:horse:
:sheep:
:monkey_face:
:bird:
:dog:
:boar:

答え

var obj = {
           x:3,
           doit: function(){ console.log("method is called." + this.x); }
          }
obj.doit(); //=> method is called.3

var fn = obj.doit;
fn(); //=> method is called.function (a,b,c){n(a)||v("Expected function but got %s: %s.",[m(a),a],b,Array.prototype.slice.call(arguments,2))}

// [解説]
// obj.doitが参照する関数オブジェクトをグローバル変数fnに代入
// 関数内のthis参照はグローバルオブジェクトを参照する
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