Last active
November 23, 2016 08:18
-
-
Save knknkn1162/d610a84f3b37183813f384c27e0eafb2 to your computer and use it in GitHub Desktop.
Node.jsにおけるuse strictの挙動 ref: http://qiita.com/knknkn1162/items/495b2b767f48d83a6ea5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo() { | |
console.log(typeof this); | |
} | |
function bar() { | |
"use strict"; | |
console.log(typeof this); | |
} | |
//要素ごとに型を確かめる | |
//this として関数に渡されたプリミティブ型はObjectとして取り扱われる | |
//object object object object object function object object | |
[123, "str", true, Symbol('abc'), [], ()=>{}, undefined, null].map(value => {return foo.call(value);} ); | |
//use strictをthisとして関数に渡された値は、 | |
//オブジェクトに閉じ込められることなく、そのまま関数に渡される | |
//number string boolean symbol object function undefined object | |
[123, "str", true, Symbol('abc'), [], ()=>{}, undefined, null].map(value => {return bar.call(value);} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
console.log(this); //thisはグローバルオブジェクト | |
})(); | |
(function () { | |
"use strict"; | |
console.log(this); // undefined | |
})(); | |
//ここのthisは{}:module.exportsオブジェクトなので、 | |
//console.logの引数のthisはレシーバーのオブジェクト自体,つまりmodule.exportsオブジェクトとなる。 | |
(function () { | |
console.log(this); | |
}).call(this); | |
//上記と同じ。 | |
(function () { | |
"use strict" | |
console.log(this); // this {}:module.exportsオブジェクト | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment