bredom
2/23/2019 - 8:17 PM

Objects

//Sparwdzanie ilości elementów w obiekcie
const obj = {
  a:1,
  b:3, 
  c:3
};

Object.keys(obj).length;
class Car {
    constructor({title}) {
        this.title = title;
    }

    drive() {
        return "We're driving";
    }
}

class Toyota extends Car {
    constructor(options) {
        super(options);
        this.color = options.color;
    }

    honk() {
        return 'Beep';
    }
}

const car = new Toyota({color: 'Red', title: 'Driver'});
console.log(car.title);
console.log(car.honk());
console.log(car.drive());
//Inheritance ES5
// Shape - superclass
function Shape() {
  this.x = 0;
  this.y = 0;
}

// superclass method
Shape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
  console.info('Shape moved.');
};

// Rectangle - subclass
function Rectangle() {
  Shape.call(this); // call super constructor.
}

// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();

console.log('Is rect an instance of Rectangle?',
  rect instanceof Rectangle); // true
console.log('Is rect an instance of Shape?',
  rect instanceof Shape); // true
rect.move(1, 1); // Outputs, 'Shape moved.'