define-accessor

1.0.1 • Public • Published

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])

Param Type Description
constructor function The constructor function whose prototype the accessor will be added to
prop string The property name of the accessor
[getter] function A getter function (optional)
[setter] function A setter function (optional)

License

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

Package Sidebar

Install

npm i define-accessor

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • akim-mcmath