Class, proptotype, inheritance, this

by Jorge Bustos Pereda

HTML

<pre id='log'></pre>

JavaScript

console.log('// Constructor');

var Punto = function(x,y) { 
    this.x=x||0; 
    this.y=y||0; 
}

var p = new Punto();

console.log('p', p);
console.log('p instanceof(Punto)', p instanceof(Punto));
console.log('p.constructor === Punto', p.constructor === Punto);

var OtroPunto = function(x,y) {
    var self = this;
    self.x = x||0;
    self.y = y||0;
    return self;
}

var op = new OtroPunto();

console.log('op instanceof(OtroPunto)', op instanceof(OtroPunto));
console.log('op.constructor === OtroPunto', op.constructor === OtroPunto);


console.log('// Prototipo');
Punto.prototype.mover = function(incx,incy) {
    this.x += incx;
    this.y += incy;
}

p.mover(10,10);
console.log('p, después de p.mover(10,10)',p);


console.log('// Todos los objetos heredan de Object');
console.log('p instanceof(Object)', p instanceof(Object));
console.log('op instanceof(Object)', op instanceof(Object));

console.log('// Propiedades en el prototipo');
Punto.prototype.z = 1000;
console.log('p.z', p.z)
console.log("p.hasOwnProperty('z')", p.hasOwnProperty('z'));