Last active
June 12, 2016 15:06
-
-
Save kitsuyui/5466de3ee81b6b85dabf6131480c5d2a to your computer and use it in GitHub Desktop.
ES2015 で Python の @classmethod のように static なメソッド内で自クラスを参照するには this を使えば良い? ref: http://qiita.com/kitsuyui/items/b7f1c5d7d969ba5febb2
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 X(object): | |
@classmethod | |
def create(cls): | |
return cls() | |
class Y(X): | |
pass | |
x = X.create() | |
y = Y.create() | |
print(isinstance(x, X)) | |
print(isinstance(y, X)) | |
print(isinstance(y, Y)) |
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 X { | |
static create() { | |
return new X(); | |
} | |
} | |
class Y extends X {}; | |
const x = X.create(); | |
const y = Y.create(); | |
console.log(x instanceof X); | |
console.log(y instanceof X); | |
console.log(y instanceof Y); |
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 X { | |
static create() { | |
return new this(); | |
} | |
} | |
class Y extends X { | |
static create() { | |
return new Y(); | |
} | |
} | |
const x = X.create(); | |
const y = Y.create(); | |
console.log(x instanceof X); | |
console.log(y instanceof X); | |
console.log(y instanceof Y); |
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 X { | |
static create() { | |
return new this(); | |
} | |
} | |
class Y extends X {}; | |
const x = X.create(); | |
const y = Y.create(); | |
console.log(x instanceof X); | |
console.log(y instanceof X); | |
console.log(y instanceof Y); |
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
true | |
true | |
true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment