Created
December 26, 2017 18:16
-
-
Save ihatecsv/56c04060a839d6657754247eef9804a8 to your computer and use it in GitHub Desktop.
Best practice for JS classes?
This file contains 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
//------------------------- Method 1 | |
class TestClass1{ | |
constructor(a){ | |
this.a = a; | |
} | |
static someMethod(){ | |
return new this(5); | |
} | |
getA(){ | |
return this.a; | |
} | |
} | |
var obj1 = new TestClass1(10); | |
console.log(obj1.getA()); | |
var obj2 = TestClass1.someMethod(); | |
console.log(obj2.getA()); | |
//------------------------- Method 2 | |
class TestClass2{ | |
makeFromNumber(a){ | |
this.a = a; | |
return this; | |
} | |
someMethod(){ | |
this.a = 5; | |
return this; | |
} | |
getA(){ | |
return this.a; | |
} | |
} | |
var obj3 = new TestClass2().makeFromNumber(10); | |
console.log(obj3.getA()); | |
var obj4 = new TestClass2().someMethod(); | |
console.log(obj4.getA()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment