javascript] OOP정리. override
# 6) JavaScript2014. 5. 23. 16:00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
function inherit(proto) { function F() { } F.prototype = proto; return new F; } //상속 관련! function extend(Child, Parent) { Child.prototype = inherit(Parent.prototype); Child.prototype.constructor = Child; Child.parent = Parent.prototype; } // todo : override var Pet = function (name) { this.Cry = function () { return "Cry:"; } } var Dog = function (name) { //Pet.call(this, name); Pet.apply(this, [name]); //override var _Cry = this.Cry; this.Cry = function () { var cry = _Cry(); return cry + "멍"; }; } var Cat = function (name) { //Pet.call(this, name); Pet.apply(this, [name]); //override var _Cry = this.Cry; this.Cry = function () { var cry = _Cry(); return cry + "냐옹"; }; } extend(Dog, Pet); extend(Cat, Pet); var dog = new Dog("강아지"); alert(dog.Cry()); var cat = new Cat("고양이"); alert(cat.Cry()); |
override 구현.
var Dog = function (name) {
//Pet.call(this, name);
Pet.apply(this, [name]);
//override
var _Cry = this.Cry;
this.Cry = function () {
var cry = _Cry(); // 상위 function call.
return cry + "멍";
};
}
'# 6) JavaScript' 카테고리의 다른 글
javascript] OOP정리. 네임스페이스 (0) | 2014.05.23 |
---|---|
javascript] OOP정리. 상속 (0) | 2014.05.23 |
javascript] OOP정리. static 멤버 (0) | 2014.05.23 |
javascript] OOP정리. prototype (0) | 2014.05.23 |
javascript] OOP정리. class처럼 만들기. (0) | 2014.05.23 |