JavaScriptの継承メソッドとは何ですか

醉折花枝作酒筹
リリース: 2021-04-08 18:30:48
オリジナル
2050 人が閲覧しました

Javascript 継承メソッドは次のとおりです: 1. call() メソッドを使用すると、さまざまなオブジェクトで使用できるメソッドを作成できます; 2. apply() メソッド、構文「apply (このオブジェクトとして使用される)」 、関数パラメータの配列に渡す必要があります)」。

JavaScriptの継承メソッドとは何ですか

このチュートリアルの動作環境: Windows 7 システム、JavaScript バージョン 1.8.5、Dell G3 コンピューター。

1. call() メソッド

call() メソッドは、古典的なオブジェクト偽装メソッドに最もよく似たメソッドです。その最初のパラメータは this のオブジェクトとして使用されます。他のパラメータは関数自体に直接渡されます

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 です。() メソッドには 2 つのパラメータしかありません。1 つ目は this のオブジェクトとして使用され、2 つ目は関数に渡されるパラメータの配列です。つまり、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 ビデオ チュートリアル ]

以上がJavaScriptの継承メソッドとは何ですかの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!