퇴근5분전

 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 + "멍";
            };
        }