Last active
July 22, 2018 14:22
-
-
Save oahehc/f0b349fc1644d3ec7442d5dacf51b833 to your computer and use it in GitHub Desktop.
javascript: this
This file contains hidden or 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 func = { | |
| f1: function() { | |
| console.log('f1', this); | |
| var sub = function () { | |
| console.log('sub', this); | |
| }; | |
| sub(); | |
| }, | |
| f2: () => { | |
| console.log('f2', this); | |
| }, | |
| } | |
| func.f1() // f1 func, sub window | |
| func.f2() // f2 window | |
| var obj = {} | |
| func.f1.call(obj) // f1 obj, sub window | |
| func.f2.call(obj) // f2 window | |
| // -------------------- | |
| var c = function() { | |
| this.a = 'a'; | |
| console.log(this); | |
| } | |
| c() // this = window | |
| var nc = new c() // this = nc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment