-
-
Save boopathi/1013910 to your computer and use it in GitHub Desktop.
Creating Prototype objects with JavaScript
This file contains hidden or 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
// 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