extend-accessors
v1.0.1
Published
extend objects with accessors without poofing
Downloads
5
Maintainers
Readme
extend-accessor
extend objects with accessors without poofing
Inspiration from Lea Verou's article Copying object properties, the robust way
install
$ npm install extend-accessor
usage
var extend = require('extend-accessor')
var Person = function (first, last) {
this.firstName = first
this.lastName = last
}
Object.defineProperty(Person.prototype, 'fullName', {
get: function () {
return this.firstName + ' ' + this.lastName
},
set: function (value) {
var parts = value.trim().split(' ')
if (parts.length === 2) {
this.firstName = parts[0]
this.lastName = parts[1]
}
}
})
var person = new Person('Johnny', 'Depp')
extend(person, { fullName: 'Monty Python' })
// person => Person { firstName: 'Monty', lastName: 'Python' }