LoginSignup
398
391

More than 5 years have passed since last update.

じゃあ this の抜き打ちテストやるぞー

Last updated at Posted at 2013-04-17

JavaScriptのthisの覚え方JavaScriptの「this」は「4つ」だけ!の授業でやったから、this はみんなばっちりだよな。じゃあ今から抜き打ちテストするぞー。まだ読んでないひとは先に上の記事を読んどくといいと思うけど、腕に自信のある人はすぐに回答を始めても構わないぞ。赤点とった奴は、今日の放課後補習だからなー。

【注意】 問題 18 ~20 について、カンマ演算子 を知らないから解けなかった、という人が結構いるみたいです。本問はカンマ演算子の知識を問うものではなく、あくまで this の振る舞いについての理解を試すものなので、本来の題意を損なわないように当該の問題は改題しました。改題後も正答とその根拠は変わりません。

得点 評価
0 ~ 5 テスト中に寝るんじゃない
6 ~ 11 鉛筆転がしのほうがマシ
12 ~ 15 平凡な一般市民
16 ~ 19 10 人に一人の逸材
20 ~ 21 this の世界的権威
22 あなたが神か

問 以下の JavaScript プログラムをウェブブラウザ上で実行したとき、true が出力されるものには ○ 、そうでないものには × をつけよ。(各1点)

1

console.log(this === window);

答え

2

'use strict';
console.log(this === window);

答え

3

function hoge(){
    console.log(this === window);
}

hoge();

答え

4

var piyo = { 
    hoge: function(){
        console.log(this === window);
    } 
};

piyo.hoge();

答え

5

function Hoge(){
    console.log(this === window);
}    

new Hoge();

答え

6

var hoge = {
    Hoge: function(){
        console.log(this === window);
    }    
};

new hoge.Hoge();

答え

7

console.log((function(){ return this; })() === window);

答え

8

'use strict';
console.log((function(){ return this; })() === window);

答え

9

function hoge(){
    console.log(this === window);
}

hoge.apply(window);

答え

10

function hoge(){
    console.log(this === window);
}

hoge.apply(42);

答え

11

function hoge(){
    console.log(this === window);
}

hoge.apply();

答え

12

'use strict';
function hoge(){
    console.log(this === window);
}

hoge.apply();    

答え

13

function hoge(){
    console.log(this === window);
}

hoge.apply(undefined, []);

答え

14

console.log(eval('this') === window);

答え

15

'use strict';
console.log(eval('this') === window);

答え

16

(function(){
    console.log(eval('this') === window);
})();

答え

17

'use strict';
(function(){
    console.log(eval('this') === window);
})();

答え

18

var _eval = eval;
console.log(_eval('this') === window);

答え ( 改題前の問題と答え )

19

'use strict';
var _eval = eval;
console.log(_eval('this') === window); 

答え ( 改題前の問題と答え )

20

'use strict';
(function(){
    var _eval = eval;
    console.log(_eval('this') === window); 
})();

答え ( 改題前の問題と答え )

21

console.log(new Function('return this')() === window);

答え

22

'use strict';
console.log(new Function('return this')() === window);    

答え

398
391
3

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
398
391