1.0.1 • Published 8 years ago

define-accessor v1.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

Define Accessor

Version License Build Coverage Dependencies

Define Accessor is a simple utility for defining a getter/setter in ES5 with the same property descriptor as an ES2015 class accessor.

Install

Install with npm:

npm install --save define-accessor

Usage

In ES2015, we might do this:

class Octopus {
  constructor(name) {
    this.name = name;
  }
  get nameEqualsTentacles() {
    return this.name.length === 8;
  }
}

In ES5, we can do this:

var defineAccessor = require('define-accessor');

function Octopus(name) {
  this.name = name;
}

defineAccessor(Octopus, 'nameEqualsTentacles', function() {
  return this.name.length === 8;
});

And here is Vladimir:

new Octopus('Vladimir').nameEqualsTentacles; // -> true

API

defineAccessor(constructor, prop, [getter], [setter])

ParamTypeDescription
constructorfunctionThe constructor function whose prototype the accessor will be added to
propstringThe property name of the accessor
getterfunctionA getter function (optional)
setterfunctionA setter function (optional)

License

Copyright © 2016 Akim McMath. Licensed under the MIT License.