Skip to content

Instantly share code, notes, and snippets.

@jorge-lavin
Created January 20, 2016 09:13
Show Gist options
  • Save jorge-lavin/ad4f4ef346e33ddc7ec5 to your computer and use it in GitHub Desktop.
Save jorge-lavin/ad4f4ef346e33ddc7ec5 to your computer and use it in GitHub Desktop.
Javascript prototypes
// Prototype demonstration
function Salutator(name) {
if (name === undefined){
throw "Please specify a name for Salutator";
} else {
this.name = name;
}
}
Salutator.prototype.setName = function(name) {
this.name = name;
};
Salutator.prototype.getName = function(name) {
return this.name;
};
Salutator.prototype.hi = function() {
var greetings = "Hello there " + this.name;
alert(greetings);
};
Salutator.prototype.bye = function() {
var goodbye = "Farewell " + this.name;
alert(goobye);
};
try {
someSalutator = new Salutator("foo");
console.log(someSalutator.constructor);
someSalutator.hi();
} catch(err) {
console.log("Error: " + err);
}
console.log("Ending");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment