すべての変数を「パブリック」にしない限り、つまりFunction、直接またはprototypeプロパティを介してそれらをメンバーにします。
var C = function( ) {
this.x = 10 , this.y = 20 ;
this.modify = function( ) {
this.x = 30 , this.y = 40 ;
console.log("(!) C >> " + (this.x + this.y) ) ;
} ;
} ;
var A = function( ) {
this.modify = function( ) {
this.x = 300 , this.y = 400 ;
console.log("(!) A >> " + (this.x + this.y) ) ;
} ;
} ;
A.prototype = new C ;
var B = function( ) {
this.modify = function( ) {
this.x = 3000 , this.y = 4000 ;
console.log("(!) B >> " + (this.x + this.y) ) ;
} ;
} ;
new C( ).modify( ) ;
new A( ).modify( ) ;
new B( ).modify( ) ;
いくつかの変更に気付くでしょう。
最も重要なのは、想定される「スーパークラス」コンストラクターの呼び出しが、この行内で暗黙的に行われるようになったことです。
<name>.prototype = new C ;
どちらAとB今持っているだろう、個別に変更可能なメンバーをxしてyいるが、我々が書かれているかどうケースではないでしょう... = C代わりに。
その後、x、yおよびmodifyすべての「パブリック」メンバーであるので、別のを割り当てることFunction、それらに
<name>.prototype.modify = function( ) { }
Functionその名前でオリジナルを「上書き」します。
最後に、想定される「スーパークラス」を想定される「サブクラス」のプロパティに設定すると、「スーパークラス」への暗黙的な呼び出しが再度実行されるためmodify、Function宣言での呼び出しを行うことはできませんprototype。
しかし、まあ、これは多かれ少なかれ、JavaScriptでこの種のことを行う方法です。
HTH、
FK
modifyはメソッドではなく入れ子関数です