curtis1000
3/1/2014 - 11:45 PM

Wasting Memory

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);