Skip to content

Instantly share code, notes, and snippets.

@nebiros
Created February 9, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebiros/8ca4592a0487cebdf7ef to your computer and use it in GitHub Desktop.
Save nebiros/8ca4592a0487cebdf7ef to your computer and use it in GitHub Desktop.
function Blah() { // función constructora, constructor o clase
this.value = 42; // atributo
}
Blah.prototype = { // atributo 'prototype'
constructor: Blah, // atributo 'constructor'
method: function() { // método
return "hiya!";
}
};
function Foo() {} // función constructora, constructor o clase
Foo.prototype = new Blah(); // heredamos del objeto 'Blah' asignando una instancia de este al atributo 'prototype'
Foo.prototype.constructor = Foo; // 'constructor'
// una instancia de Foo
var foo = new Foo();
// el valor de nuestro atributo 'value' que heredamos de 'Blah'
console.log(foo.value); // 42
// lo que retorna nuestro método 'method' que heredamos de 'Blah'
console.log(foo.method()); // hiya!
// instanceof
console.log(Foo.prototype instanceof Blah); // true
// uno de los métodos que heredamos de 'Object'
console.log(foo.toString()); // [object Object]
// el tipo de 'method'
console.log(typeof foo.method); // function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment