2016-04-26 8 views
2

私はオブジェクトオリエンテッドプログラミングが初めてであることを覚えておいてください。JS継承の失敗を管理する

function Car() { 

    var __registration; 

    var setReg = function(val) { 
     __registration= val; 
    } 

    var getReg= function() { 
     return __registration; 
    } 


    return { 
     setReg: setReg , 
     getReg: getReg 

    } 

} 

var myCar = new Car(); 
myCar.setReg("LSKM5215"); 
alert(myCar.getReg()); //ALERTS LSKM5215 

が、オブジェクト指向プログラミングのこのように継承を管理しようとすると、それだけで一度、再び失敗します:私はここに示した第一の部分がどのように機能するかを理解している(それはありません)

function Extras(){ 

    var __sound; 

    var setSound= function(val) { 
     __sound= val; 
    } 

    var getSound= function() { 
     return __sound; 
    } 


    return { 
     setSound: setSound, 
     getSound: getSound 

    } 

} 

Extras.prototype = new Car(); 

myCar.setSound("SUPERB SOUNDSYSTEM 2.2"); //TypeError: myCar.setSound is not a function 

このケースではどのようにして継承を作成できますか? Car()に"サウンドシステムエキストラ"に関するプライベート変数を取得させるには

非常に感謝します。

+0

を使用することができますarは2番目の例では – Liam

+3

「Extras」は使用しません。 –

+0

コンストラクタとして機能を使用するときに 'return'は不要 – Grundy

答えて

1

計画使用がコンストラクタとして機能する場合、returnは必要ありません。

派生クラスでは、必要なパラメータを持つ基本コンストラクタを呼び出す必要があります。

派生クラスでは、基本プロトタイプに基づいてプロトタイプを割り当てます。

このような何か:

function Car() { 
 

 
    var __registration; 
 

 
    this.setReg = function(val) { 
 
    __registration = val; 
 
    } 
 

 
    this.getReg = function() { 
 
    return __registration; 
 
    } 
 

 
} 
 

 
function Extras() { 
 
    Car.call(this); 
 
    var __sound; 
 

 
    this.setSound = function(val) { 
 
    __sound = val; 
 
    } 
 

 
    this.getSound = function() { 
 
    return __sound; 
 
    } 
 

 
} 
 

 
Extras.prototype = Object.create(Car.prototype); 
 

 
myExtras = new Extras(); 
 
myExtras.setReg("LSKM5215"); 
 
myExtras.setSound("SUPERB SOUNDSYSTEM 2.2"); 
 

 
document.write("<div>Extras: reg - ", myExtras.getReg(), '</div>'); 
 
document.write("<div>Extras: sound - ", myExtras.getSound(), '</div>');

とES2015のためにあなたは、あなたがMYCに何かを割り当てることはありませんclasses

class Polygon { 
 
    constructor(height, width) { 
 
    this.height = height; 
 
    this.width = width; 
 
    } 
 

 
    get area() { 
 
    return this.calcArea(); 
 
    } 
 

 
    calcArea() { 
 
    return this.height * this.width; 
 
    } 
 
} 
 

 
class Car { 
 

 
    constructor() { 
 
    this.__registration = undefined; 
 
    } 
 

 
    set Reg(val) { 
 
    this.__registration = val; 
 
    } 
 

 
    get Reg() { 
 
    return this.__registration; 
 
    } 
 

 
} 
 

 
class Extras extends Car { 
 

 
    constructor() { 
 
    super(); 
 
    this.__sound = undefined; 
 
    } 
 

 
    set Sound(val) { 
 
    this.__sound = val; 
 
    } 
 

 
    get Sound() { 
 
    return this.__sound; 
 
    } 
 

 
} 
 

 
myExtras = new Extras(); 
 
myExtras.Reg = ("LSKM5215"); 
 
myExtras.Sound = ("SUPERB SOUNDSYSTEM 2.2"); 
 

 
document.write("<div>Extras: reg - ", myExtras.Reg, '</div>'); 
 
document.write("<div>Extras: sound - ", myExtras.Sound, '</div>');

関連する問題