Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

fnky/extend-accessors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

extend-accessor

extend objects with accessors without poofing

browser support build status

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' }

license

MIT

About

extend objects with accessors without poofing

Resources

License

Stars

Watchers

Forks

Packages

No packages published