define-accessor
v1.0.1
Published
Define an ES2015 class getter/setter in ES5
Downloads
5
Maintainers
Readme
Define Accessor
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.