Created
January 20, 2016 09:13
-
-
Save jorge-lavin/ad4f4ef346e33ddc7ec5 to your computer and use it in GitHub Desktop.
Javascript prototypes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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