javascript継承とExt継承小記


Rectangles類の定義及びサブクラスのSquareの定義

var Rectangle = function(config){
    
    //console.log( this)


    this.width = config.width;
    this.height = config.height;

    //console.log( this)
}
Rectangle.prototype = {
    name: '',
    getArea: function(){
        return this.width * this.height;
    },
    getWidth: function(){
        return this.width;
    }
}

var rec = new Rectangle({width: 100, height: 200});

//console.log( rec )

var area = rec.getArea();

Square = function(config){

    this.width = config.edage;
    this.height = config.edage;

}

Square.prototype = new Rectangle({width:1, height:1})
Square.prototype.getA = function(){
    return 'A';

}

var s = new Square({edage: 40})

console.log( s.getArea() )

if(s instanceof Rectangle){
    console.log( 'success' )
}