Revealing protype pattern
by spring1975
HTML
<div id="output"></div>
<button id="JohnButton">John</button>
<button id="JaneButton">Jane</button>
JavaScript
// Define a class like this
function Person(name) {
// Add object properties like this
this.name = name;
}
// Add methods like this
Person.prototype = function() {
// Private method
var speak = function() {
$('#output').text('Hi, my name is ' + this.name);
};
// Public method
return {
say: speak
};
}(); // Execute the function for the prototype to become the returned object
// Instantiate new objects with 'new'
var person = new Person('John');
// Invoke methods like this
person.say();
person.speak(); // undefined