Skip to content

Instantly share code, notes, and snippets.

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 boopathi/1013910 to your computer and use it in GitHub Desktop.
Save boopathi/1013910 to your computer and use it in GitHub Desktop.
Creating Prototype objects with JavaScript
// Defining constructor function
function ObjectConstructor(message) {
// TODO: Add your own initialization code here
this.message = message || 'Hello Prototype World!';
};
// Defining an instance function
ObjectConstructor.prototype.sayHello = function() {
alert(this.message);
};
//In this way, you can set multiple functions
//Avoids writing ObjectConstructor.prototype everytime while defining a function
ObjectContructor.prototype = {
sayHello: function() {
alert(this.message);
},
setMessage: function(message) {
this.message = message;
}
};
// Using your Prototype object
var object = new ObjectConstructor();
object.sayHello();
var object = new ObjectConstructor('Hello Mexpolk!');
object.sayHello();
object.setMessage("Hello Boopathi");
object.sayHello();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment