Created
June 8, 2020 13:41
-
-
Save jeremiah-ang/42d07b28c6d563a644c2c3a77141270d to your computer and use it in GitHub Desktop.
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
const a = { | |
foo: function (target) { | |
return this === target; | |
}, | |
bar: function() { | |
return (target) => this === target; | |
} | |
} | |
b = { | |
foo: a.foo, | |
bar: a.bar, | |
} | |
b.bar = b.bar(); | |
c = { | |
foo: function (target) { | |
return a.foo(target) | |
}, | |
bar: a.bar(), | |
} | |
d = {} | |
// Replace d with a,b,c or global into the parameter to get all printed as true | |
// | |
// New concept 1: Global "This" | |
// The global scope is one huge object as well, thus it may be assigned to a "this" of some function too. | |
// the global "this" is called `global` | |
// e.g. global === this; | |
// | |
// New Concept 2: () => {} arrow functions | |
// Takes the this of the context in which it was defined | |
// | |
const foo = a.foo | |
console.log(foo(d)) | |
console.log(a.foo(d)) | |
console.log(a.bar(/* left blank intentionally */)(d)) | |
console.log(b.foo(d)) | |
console.log(b.bar(d)) | |
console.log(c.foo(d)) | |
console.log(c.bar(d)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment