Skip to content

Instantly share code, notes, and snippets.

@vrdhn
Last active March 21, 2022 11:37
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 vrdhn/239bce4c1b2fb5c77ca19bf0b2aea865 to your computer and use it in GitHub Desktop.
Save vrdhn/239bce4c1b2fb5c77ca19bf0b2aea865 to your computer and use it in GitHub Desktop.
What is this
const root = (typeof window === 'undefined') ? global : window;
function LambdaClosure() {
return () => {
if ( this == root )
console.log('Closure: this is global/window');
else
console.log('Closure: this is ', this.name);
};
}
function LambdaFunction() {
return function () {
if ( this == root )
console.log('Function: this is global/window');
else
console.log('Function: this is ', this.name);
};
}
console.log("---- take 1 -----")
LambdaFunction()()
LambdaClosure()()
console.log("---- take 2 -----")
let obj = { name: 'first' };
obj.f = LambdaFunction;
obj.c = LambdaClosure;
obj.f()()
obj.c()()
console.log("---- take 3 -----")
let obj2 = { name: 'second' };
obj2.f = LambdaFunction();
obj2.c = LambdaClosure();
obj2.f()
obj2.c()
console.log("---- take 4 -----")
let obj3 = { name: 'third' };
let obj4 = { name: 'fourth' };
obj3.f = LambdaFunction
obj3.c = LambdaClosure
obj4.f = obj3.f();
obj4.c = obj3.c();
obj4.f()
obj4.c()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment