Last active
November 23, 2016 05:33
-
-
Save knknkn1162/6db834f14ce2dc52fae51eb96ca28e8a to your computer and use it in GitHub Desktop.
JavaScriptにおけるthisの挙動まとめ(Node.js) ref: http://qiita.com/knknkn1162/items/0fa7dcb6a735125d21db
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
console.log(this);//{} : module.exports Object | |
this.obj = "a" | |
console.log(this); // { obj: 'a' } | |
//cf) 2 : 関数呼び出しも参考にしてください | |
var func = function() | |
{ | |
console.log(this); | |
}; | |
func(); //thisはグローバルオブジェクトを指す。 |
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
var obj1 = | |
{ | |
p0 : this, | |
//外部関数 | |
p1 :function() {return this; }, | |
//入れ子(内部)関数 | |
p2 : function() { return function() {return this;} }, | |
}; | |
//thisはmodule.exportオブジェクトをさす | |
console.log(obj1.p0); //{} | |
//thisはobj1をさす | |
console.log(obj1.p1()); | |
//thisはグローバルオブジェクトをさす(上記の本の4.3.2には、「言語の設計としては失敗」と書いてある!) | |
console.log(obj1.p2()()); |
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
var obj1 = new Object(); | |
obj1.p0 = this; | |
obj1.p1 = function() { return this;}; | |
obj1.p2 = function() { return function() { return this; }}; |
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
//call関数の引数thisはグローバルオブジェクトをさす | |
//console.log内の引数のthisは(インスタンス)メソッド呼び出しに該当するので、 | |
//同様にレシーバーのオブジェクト(つまりグローバルオブジェクト)となる | |
(function () { | |
console.log(this); | |
}).call(this); |
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
var Member = function() | |
{ | |
this.name= 'a'; | |
} | |
//関数とみなして呼び出すと,thisがグローバルオブジェクトとなる。 | |
var inst = Member(); | |
//thisはグローバルオブジェクトをさしてしまうため、nameが参照できてしまう | |
console.log(name); |
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
var obj2 = | |
{ | |
p0 : this, //{} | |
p1 : function() {return this; }, //obj2をさす.上記参照 | |
//p0と状況が同じ。 | |
p1_2 : () => {return this;}, | |
//入れ子になっているが、p0と状況は同じ。 | |
p2 : () => { return () => {return this; } }, | |
//アロー関数自体はfunction()内部にあるので、p1と状況は同じ | |
p3 : function() { return () => {return this; }}, | |
//アロー関数を入れ子関数にしてみる | |
p4 : function() { return () => { return () => {return this; }; } }, | |
}; | |
//thisはmodule.exportオブジェクトをさす(関数リテラル表現のときと挙動が違うことに注意。) | |
console.log(obj2.p1_2()); | |
//thisはmodule.exportオブジェクト{}をさす | |
console.log(obj2.p2()()); | |
//thisはobj1をさす | |
console.log(obj2.p3()()); | |
//thisはobj1をさす(つまり、p3の時のthisと同じ参照先。) | |
console.log(obj2.p4()()()); | |
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
var Member = function(name) | |
{ | |
this.name = name; | |
}; | |
Member.prototype.getName = function() | |
{ | |
//thisは生成されたインスタンスを指す | |
console.log(this); | |
return this.name; | |
} | |
var mem = new Member("KN"); | |
console.log(mem.getName()); //KN |
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
var Member = function(arg1) { this.p1 = arg1}; | |
Member.method = function() { return this;} | |
//関数オブジェクト(この場合はMemberオブジェクト自体)を指す | |
console.log(Member.method()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment