JSFiddle - React, Tailwind, and code Playground
JavaScript
function Color() {
this.color = 'red';
this.getColor = function getColor() {
console.log(this.color);
};
}
const b = new Color();
b.color = 'blue';
b.getColor();
// function AnotherColor() {
// this.color = 'red';
// this.getColor = function getColor() {
// return setTimeout(() => console.log(this.color), 2000);
// };
// }
function AnotherColor() {
this.color = 'red';
this.getColor = function getColor() {
const self = this;
return setTimeout(function () {
console.log(self.color);
}, 2000);
};
}
const c = new AnotherColor();
c.color = 'green';
c.getColor(); // green