Last active
July 3, 2016 03:14
-
-
Save gaogao-9/418c3d13baf9f5c466f6644aa2261950 to your computer and use it in GitHub Desktop.
クラスの複製デザパタです
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 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