Extension methods in JavaScript

by douglasloyo

JavaScript

Object.defineProperty(Object.prototype, "cleanAddress", {
    value: function cleanAddress() {
    		this.city = {id:this.city.id};
        this.state = {id:this.state.id};
    }
});

var address = {
street: "107 glory rd",
city: {id:1, name:"JonesTown"},
state: {id:1, code:"Tx", name: "Texas"}
};

address.cleanAddress();

alert(JSON.stringify(address));