Point ES6 class - Node.js Object

Node.js examples for Object:Class in ES6

Description

Point ES6 class

Demo Code


//es6//from   ww w . j  a  v  a2s .  c  om

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}


class ColorPoint extends Point {
  constructor(x, y, color) {
    super(x, y); 
    this.color = color;
  }

  toString() {
    return this.color + ' ' + super.toString(); 
  }
}


var p = new ColorPoint(1, 2,'red');

console.log(p.toString())


function inherit(superType, subType){
    var _prototype  = Object.create(superType.prototype);
    _prototype.constructor = subType; 
    subType.prototype = _prototype;
}

function Person(name, sex){
    this.name = name;
    this.sex = sex;
}

Person.prototype.printName = function(){
    console.log(this.name);
};  

function Male(name, sex, age){
    Person.call(this, name, sex);
    this.age = age;
}
inherit(Person, Male);

Male.prototype.printAge = function(){
    console.log(this.age);
};

var m = new Male('Byron', 'm', 26);
m.printName();

Related Tutorials