Skip to content

Instantly share code, notes, and snippets.

@kitsuyui
Last active June 12, 2016 15:06
Show Gist options
  • Save kitsuyui/5466de3ee81b6b85dabf6131480c5d2a to your computer and use it in GitHub Desktop.
Save kitsuyui/5466de3ee81b6b85dabf6131480c5d2a to your computer and use it in GitHub Desktop.
ES2015 で Python の @classmethod のように static なメソッド内で自クラスを参照するには this を使えば良い? ref: http://qiita.com/kitsuyui/items/b7f1c5d7d969ba5febb2
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))
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);
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);
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);
true
true
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment