퇴근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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  /* http://javascript.info/tutorial/pseudo-classical-pattern
     There is a well-known, but wrong way of inhereting, when instead of Rabbit.prototype = inherit(Animal.prototype) people use: 
  // inherit from Animal  
  Rabbit.prototype = new Animal()  
  As a result, we get a new Animal object in prototype. Inheritance works here, because new Animal naturally inherits Animal.prototype. 
  … But who said that new Animal() can be called like without the name? The constructor may strictly require arguments and die without them. 
  Actually, the problem is more conceptual than that. We don’t want to create an Animal. We just want to inherit from it. 
  That’s why Rabbit.prototype = inherit(Animal.prototype) is preferred. The neat inheritance without side-effects.  
     */

          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 : 상속!
          var Pet = function (name) {
              // 여기서 name은 private
              var privateMember = 1; // private
              function getAge() { // private
              }  

              this._ProtectedMemeber = "펫"; // protected 
              this.getName = function () { return name; }
              this.setName = function (nm) { name = nm; }

              this.get = function ()
              {
                  // this._ProtectedMemeber // protected 멤버
                  return getAge(); // private 멤버, 
              }
          }

          Pet.prototype._Cry = function ()
          {
              //var age = this.getAge(); // private로 호출할수 없다.
              return "Pet._Cry" + this._ProtectedMemeber;
          }

          Pet.prototype.Cry = function ()
          {
              return this._Cry();// protected인지 Dog.Prototype에서 보자.. 
          }


          var Dog = function (name) { 
              Pet.call(this, name); 
              //Pet.apply(this, [name]);
             this._ProtectedMemeber = "멍"; 
          }
        //  Dog.prototype = inherit(Pet.prototype);// 상속!
          //  Dog.constructor = Dog;
          extend(Dog, Pet);
          Dog.prototype.WW = function ()
          { 
              var mm = this._ProtectedMemeber; 
              return this._Cry(); // 오!!!! 
          }

          var pet = new Pet("애완");
          pet.Cry(); // pet._Cry(); and pet._ProtectedMemeber 는 나타나지 않음! 
       
          var dog = new Dog("강아쥐");

          alert(dog.getName()+ " : " + dog.WW());

          // (dog is Dog)? yes
          alert(dog instanceof Dog);
          // (dog is Pet)? yes
          alert(dog instanceof Pet);
          // (dog is Object)? yes
          alert(dog instanceof Object);

 

상속을 만들었다.

inherit 과 extend function을 이용한다.

 

위 예는 Pet 을 상속구현한 Dog.

그리고 protected member 상속 여부 확인. ( _Cry() 와 _ProtectedMemeber )

 

var Dog = function (name) {
              Pet.call(this, name);
              //Pet.apply(this, [name]);
             this._ProtectedMemeber = "멍";
          }

이부분에서 Pet.call( this, name) 단일변수형태 - .call

                Pet.apply(this, [name]); 배열일때  - .apply