Js 객체 중심 구조 함수 상속

2305 단어
구조 함수 계승

  

  function Animal(){
    this.species= ' ';
  }
  function Dog(name,color){
    this.name = name;
    this.color = color;
  }

prototype 모드:


만약'개'의 프로토타입 대상이 하나의 애니멀의 실례를 가리킨다면 모든'개'의 실례는 애니멀을 계승할 수 있을 것이다.
  
  Dog.prototype = new Animal();
  Dog.prototype.constructor = Dog; // Dog.prototype constructor Dog
  var dog1 = new Dog(" "," ");
  alert(dog1.species); //  

빈 객체를 브로커로 사용하려면 다음과 같이 하십시오.
빈 대상을 중개로 이용하다.
  var F = function(){};
  F.prototype = Animal.prototype;
  Cat.prototype = new F();
  Cat.prototype.constructor = Cat;

  // F , 。 , Cat prototype , Animal prototype 。

우리는 위의 방법을 하나의 함수로 봉하여 사용하기 편리하게 할 것이다.
  function extend(Child, Parent) {
    var F = function(){};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.uber = Parent.prototype;
  }

  
    extend(Dog,Animal);
    var dog1= new Dog(" "," ");
    alert(dog1.species); //

    // extend , YUI 。

 

좋은 웹페이지 즐겨찾기