Wasting Memory
/**
* This pattern works, but it wastes memory by defining the
* setName() and getName() functions for each new instance of Person
*/
var Person = function() {
var interface = {};
interface.setName = function(name) {
this.name = name;
return this;
};
interface.getName = function() {
return this.name;
};
interface.getGreeting = function() {
return 'Hi! My name is ' + this.getName();
};
return interface;
};
var joe = new Person();
joe.setName('Joseph');
var output = joe.getGreeting();
console.log(output);