Define Accessor is a simple utility for defining a getter/setter in ES5 with the same property descriptor as an ES2015 class accessor.
Install with npm:
npm install --save define-accessorIn 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| 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) |
Copyright © 2016 Akim McMath. Licensed under the MIT License.