Skip to content

Instantly share code, notes, and snippets.

@S--Minecraft
Last active November 21, 2015 17:07
Show Gist options
  • Select an option

  • Save S--Minecraft/d6b472c7e48370d7ab3f to your computer and use it in GitHub Desktop.

Select an option

Save S--Minecraft/d6b472c7e48370d7ab3f to your computer and use it in GitHub Desktop.
typescript overload
//!overload
constructor(x: number, y: number) {
this.x = a;
this.y = b;
}
constructor(v: Vector2) {
this.x = v.x;
this.y = v.y;
}
constructor(x: number, y: number);
constructor(v: Vector2);
constructor(a: any, b?: number) {
// null, undefined が渡された場合
if (a == null) {
this.x = 0;
this.y = 0;
return;
}
// instanceof でどちらが呼ばれたのか判断
if (a instanceof Vector2) {
this.x = a.x;
this.y = a.y;
} else {
this.x = a;
this.y = b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment