Last active
November 6, 2018 18:26
-
-
Save undecidedapollo/252a37b7cc12e16c7c7639e671853fbb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//Doesnt work | |
class MyClass { | |
constructor(value) { | |
this.val = value; | |
} | |
printMessage() { | |
console.log("CLASS", this.val); | |
} | |
} | |
function MyOtherClass(value) { | |
this.val = value; | |
} | |
MyOtherClass.prototype.printMessage = function() { | |
console.log("MyOtherClass", this.val); | |
} | |
//Works | |
class MyBindedClass { | |
constructor(value) { | |
this.val = value; | |
this.printMessage = this.printMessage.bind(this); | |
} | |
printMessage() { | |
console.log("MyBindedClass", this.val); | |
} | |
} | |
function MyNewClass(value) { | |
const self = this; | |
self.val = value | |
this.printMessage = function() { | |
console.log("MyNewClass", self.val); | |
} | |
} | |
function createNewObj(val) { | |
function printMessage() { | |
console.log("CLOSURE", val); | |
} | |
return { | |
printMessage | |
}; | |
} | |
//Works | |
const myClosureObj = createNewObj(456); | |
const myNewobj = new MyNewClass(444); | |
const myBindedObj = new MyBindedClass(456); | |
//Doesnt work | |
const myObj = new MyClass(123); | |
const myOtherObj = new MyOtherClass(999); | |
//Works | |
setTimeout(myClosureObj.printMessage, 10); | |
setTimeout(myNewobj.printMessage, 10); | |
setTimeout(myBindedObj.printMessage, 10); | |
//Doesnt work | |
setTimeout(myObj.printMessage, 10); | |
setTimeout(myOtherObj.printMessage, 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment