ホームページ  >  記事  >  ウェブフロントエンド  >  JavaScript 継承メカニズムの実装に関する簡単な説明

JavaScript 継承メカニズムの実装に関する簡単な説明

高洛峰
高洛峰オリジナル
2016-11-25 15:34:54815ブラウズ

オブジェクト偽装メソッドの実装:

[javascript]
function Human() { //Human クラスを定義します
this.species = "Human" ;
}
function Sex(sex) { //セックス クラスを定義します
this.sex = sex;
function Chinese(name,sex; ) {
this.name = name;
this.newMethod1 = Human; //Human オブジェクトを指すオブジェクトの偽装
this.newMethod1(); // メソッドを呼び出して継承を実装
delete this.newMethod1;誤った呼び出しを回避するためのオブジェクト参照

this.newMethod2 = Sex; // Sex オブジェクトを指すオブジェクトの参照
this.newMethod2(sex) // メソッドを呼び出して継承を実現する
delete this.newMethod2;オブジェクトへの参照、誤った呼び出しを避ける
}
var chin1 = new Chinese("Xiao Ming", " Male")
function Human() { //人間クラスを定義
this.species = "Human";
}
function Sex(sex) { //性別クラスを定義
this.sex = sex;
}
function Chinese(name,sex) {
this.name = name;
this.newMethod1 = Human; //オブジェクトの偽装、Human オブジェクトを指します
this.newMethod1(); /継承を実現するためにメソッドを呼び出します
delete this .newMethod1; // 誤った呼び出しを避けるためにオブジェクトへの参照を削除します
this.newMethod2 = Sex オブジェクトを指します
this.newMethod2(sex); ; //継承を実現するためにメソッドを呼び出します
delete this.newMethod2 // 誤った呼び出しを避けるためにオブジェクトへの参照を削除します
}
var chin1 = new Chinese("Xiao Ming", " Male");

オブジェクトの偽装の方法は非常にシンプルでわかりやすく、関数内で属性の定義を実装します

さらに、オブジェクトの偽装には欠点もあります。以下の通り:


[javascript]