Skip to content

Instantly share code, notes, and snippets.

@knknkn1162
Last active November 23, 2016 08:18
Show Gist options
  • Save knknkn1162/d610a84f3b37183813f384c27e0eafb2 to your computer and use it in GitHub Desktop.
Save knknkn1162/d610a84f3b37183813f384c27e0eafb2 to your computer and use it in GitHub Desktop.
Node.jsにおけるuse strictの挙動 ref: http://qiita.com/knknkn1162/items/495b2b767f48d83a6ea5
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);} );
(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