Skip to content

Instantly share code, notes, and snippets.

@gaogao-9
Last active July 3, 2016 03:14
Show Gist options
  • Save gaogao-9/418c3d13baf9f5c466f6644aa2261950 to your computer and use it in GitHub Desktop.
Save gaogao-9/418c3d13baf9f5c466f6644aa2261950 to your computer and use it in GitHub Desktop.
クラスの複製デザパタです
class Human{
constructor(name, age){
this.name = name;
this.age = age;
}
static clone(obj){ return new this(obj.name, obj.age); }
}
class Woman extends Human{}
class Gao extends Human{
constructor(fav){
super("gao", 18);
this.fav = fav;
}
static clone(obj){ return new this(obj.fav); }
}
var foo = new Human("foo", 20);
console.log(foo); // Human { name: "foo", age: 20 }
console.log(Woman.clone(foo)); // Woman { name: "foo", age: 20 }
var gao = new Gao("sushi");
var gao2 = Gao.clone(gao);
gao2.fav = "beaf";
console.log(gao); // Gao { name: "gao", age: 18, fav: "sushi" }
console.log(gao2); // Gao { name: "gao", age: 18, fav: "beaf" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment