Skip to content

Instantly share code, notes, and snippets.

@jamc92
Created March 15, 2015 19:43
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 jamc92/dd88ea2b070d21b30a2f to your computer and use it in GitHub Desktop.
Save jamc92/dd88ea2b070d21b30a2f to your computer and use it in GitHub Desktop.
JS - Inheritance with prototype
<!DOCTYPE html>
<html>
<head>
<title>Prototype Inheritance</title>
<script type="text/javascript">
function Domestico() {
this.animal = "";
this.nombre= "";
this.configurarAnimal = function(nuevoAnimal) {
this.animal = nuevoAnimal;
}
this.configurarNombre= function(nuevoNombre) {
this.nombre = nuevoNombre;
}
}
var miGato = new Domestico();
miGato.ConfigurarAnimal = "gato";
miGato.ConfigurarNombre = "Silvestre";
Perro.prototype = new Domestico();
function Perro() {
this.raza = "";
this.configurarRaza = function(nuevaRaza) {
this.raza = nuevaRaza;
}
this.mostrarInfo = function() {
alert(this.nombre + " es un " + this.raza);
}
}
var miPerro = new Perro();
miPerro.configurarNombre("Montano");
miPerro.configurarRaza("San Bernardo");
</script>
</head>
<body>
<input type="button" value="Mostrar info" onclick="miPerro.mostrarInfo()" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment