>  기사  >  웹 프론트엔드  >  자바스크립트의 상속 방법은 무엇입니까?

자바스크립트의 상속 방법은 무엇입니까?

醉折花枝作酒筹
醉折花枝作酒筹원래의
2021-04-08 18:30:232045검색

Javascript 상속 방법은 다음과 같습니다. 1. call() 메서드를 사용하여 다양한 객체에 사용할 수 있는 메서드를 작성할 수 있습니다. 2. apply() 메서드, 구문 "apply(는 이 개체로 사용됩니다. 함수 배열에 전달될 매개변수)".

자바스크립트의 상속 방법은 무엇입니까?

이 튜토리얼의 운영 환경: Windows 7 시스템, JavaScript 버전 1.8.5, Dell G3 컴퓨터.

1. call() 메소드

call() 메소드는 고전적인 객체 가장 메소드와 가장 유사한 메소드입니다. 첫 번째 매개변수가 이것의 객체로 사용됩니다. 다른 매개변수는 함수 자체에 직접 전달됩니다.

function Huster(name,idNum,college)
    {        this.name = name;        
        this.idNum = idNum;        
        this.college = college;        
        this.course = new Array();    
        
        this.addCourse = function(course)//这个方法不能用prototype来定义,如果用的话,子类无法继承该方法
        {                   //用原型prototype定义的方法可以用原型链来继承,call()方法和apply()方法都无法继承            this.course.push(course);
            console.log(this.course);
        };    
            
    }    
    
    function Autoer(name,idNum)
    {        
    this.college = ‘自动化‘;
        Huster.call(this,name,idNum,this.college);//Autoer使用call()方法来继承 Huster
     if (typeof Autoer._initialized == "undefined") 
     { 
      Autoer.prototype.sayHobby = function() //自己的方法可以用原型链prototype定义
      { 
        alert(this.college+‘人喜欢撸代码!‘);     
      }; 
      Autoer._initialized = true; 
     } 
  } 
   
  var autoer1 = new Autoer(‘偶人儿‘,‘U123456789‘); //声明一个实例autoer1
  console.log(autoer1.name,autoer1.idNum,autoer1.college,autoer1.course); 
  autoer1.addCourse(‘logistics‘);//调用Huster的方法
  autoer1.sayHobby();    //调用自身的方法

2.apply() 메서드

apply() 메서드는 call() 메서드와 거의 동일합니다. 두 개의 매개변수 중 첫 번째는 이것으로 사용되는 객체이고, 두 번째는 함수에 전달될 매개변수의 배열입니다. 즉, apply() 메소드는 call() 메소드의 여러 매개변수를 배열에 넣고 이를 상위 클래스

function Huster(name,idNum,college){    
this.name = name;            
this.idNum = idNum;    
this.college = college;    
this.course = new Array();            
this.addCourse = function(course)//这个方法不能用prototype来定义,如果用的话,子类无法继承该方法    
{                   //用原型prototype定义的方法可以用原型链来继承,call()方法和apply()方法都无法继承        
this.course.push(course);        
console.log(this.course);    
};                
}            
function Autoer(name,idNum)    
{        
this.college = ‘自动化‘;        
Huster.apply(this,new Array(name,idNum,this.college));//Autoer使用apply()方法来继承 Huster     
if (typeof Autoer._initialized == "undefined")      
{       
Autoer.prototype.sayHobby = function() //自己的方法可以用原型链prototype定义      
{         
alert(this.college+‘人喜欢撸代码!‘);           
};       
Autoer._initialized = true;      
}   
}      
var autoer1 = new Autoer(‘偶人儿‘,‘U123456789‘); //声明一个实例autoer1  
console.log(autoer1.name,autoer1.idNum,autoer1.college,autoer1.course);   
autoer1.addCourse(‘logistics‘);//调用Huster的方法  
autoer1.sayHobby();    //调用自身的方法

에 전달합니다.[권장 학습: javascript 비디오 튜토리얼]

위 내용은 자바스크립트의 상속 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.