Posts

Showing posts with the label recycle

On Complex Getters And Setters

A common use case for getters and setters is via scalar values rather than complex data. Well, this is just a programmer mind limit since data we could set, or get, can be of course much more complex: here an example function Person() {} Person.prototype.toString = function () { return this._name + " is " + this._age; }; // the magic identity configuration object Object.defineProperty(Person.prototype, "identity", { set: function (identity) { // do something meaningful this._name = identity.name; this._age = identity.age; // store identity for the getter this._identity = identity; }, get: function () { return this._identity; } }); With above pattern we can automagically update a Person instance name and age through a single identity assignment. var me = new Person; me.identity = { name: "WebReflection", age: 33 }; alert(me); // WebReflection is 33 While the example may not make muc...