Skip to content

Instantly share code, notes, and snippets.

@jeremiah-ang
Created June 8, 2020 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremiah-ang/42d07b28c6d563a644c2c3a77141270d to your computer and use it in GitHub Desktop.
Save jeremiah-ang/42d07b28c6d563a644c2c3a77141270d to your computer and use it in GitHub Desktop.
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