JSFiddle - React, Tailwind, and code Playground
by TheDiamondDoge
JavaScript
/* function createCounter() {
this;
let x = 1;
return function() {
// lexical environment
let lkasdjlask = 1234;
return x++;
}
}
let counter = createCounter();
let counter2 = createCounter();
console.log(createCounter()()); //1
console.log(createCounter()()); //2
console.log(counter()); //3
//this = window
createCounter();
//this = obj1
obj1.createCounter()
//this = obj2
createCounter.bind(obj2);
let arrow = () => console.log(this);
*/
// foo();
function foo() {
//this = window
//this = {x: 500}
const x = 10;
return {
x: 20,
bar: () => console.log(this.x),
baz: function() {
console.log(this.x);
}
}
}
// foo(); this = window
// obj = {x: 500, foo: function(){}};
// obj.foo(); this = obj
// foo.call(obj); this = obj
const obj22 = {
x: 10,
bar: () => console.log(this.x),
}
obj22.bar();
const obj11 = foo();
obj11.baz();
obj11.bar();
const obj111 = foo.call({k: 500});
/*
obj111 = {
x: 20,
bar: () => console.log(this.x),
baz: function() {
console.log(this.x);
}
}
obj111.baz();
*/
obj111.baz();
obj111.bar();