JavaScript はどのように継承を実装するのでしょうか?

青灯夜游
リリース: 2021-10-13 13:39:49
オリジナル
2718 人が閲覧しました

方法: 1. プロトタイプを使用して、ある参照型に別の参照型のプロパティとメソッドを継承させます。 2. コンストラクターを借用し、call() と apply( を使用してサブクラス コンストラクター内のスーパークラス コンストラクターを呼び出します) ) 新しく作成されたオブジェクトに対してコンストラクターを実行できます; 3. プロトタイプ チェーンとコンストラクターを借用するテクノロジを組み合わせて、継承を実現します。

JavaScript はどのように継承を実装するのでしょうか?

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

前書き: ほとんどの OO 言語は、インターフェイスの継承と実装の継承という 2 つの継承方法をサポートしています。ただし、インターフェイスの継承は ECMAScript では実装できません。ECMAScript は実装の継承のみをサポートしており、その実装の継承は主に次のものに依存しています。プロトタイプチェーンを実現します。

1. プロトタイプ チェーン

基本的な考え方: プロトタイプを使用して、ある参照型に別の参照型のプロパティとメソッドを継承させます。

コンストラクター、プロトタイプ、インスタンスの関係: 各コンストラクターにはプロトタイプ オブジェクトがあり、プロトタイプ オブジェクトにはコンストラクターへのポインターが含まれ、インスタンスにはプロトタイプ オブジェクトへの内部ポインターが含まれます。

プロトタイプ チェーン実装の継承例:

function SuperType() {
this .property = true ;
}
SuperType.prototype.getSuperValue = function () {
return this .property;
}
function subType() {
this .property = false ;
}
//继承了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function (){
return this .property;
}
var instance = new SubType();
console.log(instance.getSuperValue()); //true
ログイン後にコピー

2. コンストラクターの借用

基本的な考え方: サブタイプ コンストラクター内でスーパークラス コンストラクターを呼び出します。コンストラクターは、call() メソッドと apply() メソッドを使用して、新しく作成されたオブジェクトに対して実行できます。

例:

function SuperType() {
this .colors = [ "red" , "blue" , "green" ];
}
function SubType() {
SuperType.call( this ); //继承了SuperType
}
var instance1 = new SubType();
instance1.colors.push( "black" );
console.log(instance1.colors); //"red","blue","green","black"
var instance2 = new SubType();
console.log(instance2.colors); //"red","blue","green"
ログイン後にコピー

3. 結合された継承

基本的な考え方: プロトタイプ チェーンとコンストラクターを借用するテクノロジを結合する、継承両方の長所を生かしたモデル。

例:

function SuperType(name) {
this .name = name;
this .colors = [ "red" , "blue" , "green" ];
}
SuperType.prototype.sayName = function () {
console.log( this .name);
}
function SubType(name, age) {
SuperType.call( this ,name); //继承属性
this .age = age;
}
//继承方法
SubType.prototype = new SuperType();
Subtype.prototype.constructor = Subtype;
Subtype.prototype.sayAge = function () {
console.log( this .age);
}
var instance1 = new SubType( "EvanChen" ,18);
instance1.colors.push( "black" );
consol.log(instance1.colors); //"red","blue","green","black"
instance1.sayName(); //"EvanChen"
instance1.sayAge(); //18
var instance2 = new SubType( "EvanChen666" ,20);
console.log(instance2.colors); //"red","blue","green"
instance2.sayName(); //"EvanChen666"
instance2.sayAge(); //20
ログイン後にコピー

4. プロトタイプの継承

基本的な考え方: プロトタイプを使用すると、以下に基づいて新しいオブジェクトを作成できます。既存のオブジェクトも使用でき、カスタム タイプを作成する必要もありません。

プロトタイプ継承の概念は、次の関数で説明できます:

function object(o) {
function F(){}
F.prototype = o;
return new F();
}
ログイン後にコピー

例:

var person = {
name: "EvanChen" ,
friends:[ "Shelby" , "Court" , "Van" ];
};
var anotherPerson = object(person);
anotherPerson.name = "Greg" ;
anotherPerson.friends.push( "Rob" );
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda" ;
yetAnotherPerson.friends.push( "Barbie" );
console.log(person.friends); //"Shelby","Court","Van","Rob","Barbie"
ログイン後にコピー

ECMAScript5 は、新しい Object.create() メソッドを通じてプロトタイプ継承を標準化します。このメソッドは、新しいオブジェクトのプロトタイプとして機能するオブジェクトと、新しいオブジェクトの追加プロパティを定義するオブジェクトという 2 つのパラメーターを受け入れます。

var person = {
name: "EvanChen" ,
friends:[ "Shelby" , "Court" , "Van" ];
};
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg" ;
anotherPerson.friends.push( "Rob" );
var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda" ;
yetAnotherPerson.friends.push( "Barbie" );
console.log(person.friends); //"Shelby","Court","Van","Rob","Barbie"
ログイン後にコピー

5. 寄生継承

基本的な考え方: 継承プロセスをカプセル化するためにのみ使用される関数を作成し、何らかの方法で内部的に使用されます。オブジェクトを取得し、最後に実際にすべての作業を行ったかのようにオブジェクトを返します。

例:

function createAnother(original) {
var clone = object(original);
clone.sayHi = function () {
alert( "hi" );
};
return clone;
}
var person = {
name: "EvanChen" ,
friends:[ "Shelby" , "Court" , "Van" ];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi(); ///"hi"
ログイン後にコピー

6. 寄生結合継承

基本的な考え方: プロトタイプ チェーンを通じて、関数を借用してプロパティを継承する混合形式メソッドを継承するための

基本モデルは次のとおりです:

function inheritProperty(subType, superType) {
var prototype = object(superType.prototype); //创建对象
prototype.constructor = subType; //增强对象
subType.prototype = prototype; //指定对象
}
ログイン後にコピー

例:

function SuperType(name){
this .name = name;
this .colors = [ "red" , "blue" , "green" ];
}
SuperType.prototype.sayName = function (){
alert( this .name);
};
function SubType(name,age){
SuperType.call( this ,name);
this .age = age;
}
inheritProperty(SubType,SuperType);
SubType.prototype.sayAge = function () {
alert( this .age);
}
ログイン後にコピー

[推奨学習:JavaScript 上級チュートリアル ]

以上がJavaScript はどのように継承を実装するのでしょうか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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