Skip to content

Instantly share code, notes, and snippets.

@oahehc
Last active July 22, 2018 14:22
Show Gist options
  • Save oahehc/f0b349fc1644d3ec7442d5dacf51b833 to your computer and use it in GitHub Desktop.
Save oahehc/f0b349fc1644d3ec7442d5dacf51b833 to your computer and use it in GitHub Desktop.
javascript: this
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