-
-
Save getify/6ca9237ecdc4044ac334a40c00f9a1fd to your computer and use it in GitHub Desktop.
class (with bound method) vs module
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
class Foo { | |
constructor(x) { | |
this.x = x; | |
this.hello = () => { | |
console.log(this.x); | |
}; | |
} | |
} | |
var a = new Foo(12); | |
a.hello(); // 12 | |
var b = new Foo(20); | |
b.hello(); // 20 |
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
function Foo(x) { | |
return { | |
hello() { console.log(x); } | |
}; | |
} | |
var a = Foo(12); | |
a.hello(); // 12 | |
var b = Foo(20); | |
b.hello(); // 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment