define-method

1.0.1 • Public • Published

Define Method

Version License Build Coverage Dependencies

Define Method is a simple utility for defining an instance method in ES5 with the same property descriptor as an ES2015 class method.

Install

Install with npm:

npm install --save define-method

Usage

In ES2015, we might do this:

class Fish {
  constructor(name) {
    this.name = name;
  }
  greet() {
    console.log(`Hello, my name is ${this.name} the fish.`);
  }
}

In ES5, we can do this:

var defineMethod = require('define-method');
 
function Fish(name) {
  this.name = name;
}
 
defineMethod(Fish, 'greet', function() {
  console.log('Hello, my name is ' + this.name + ' the fish.');
});

And here is Vladimir:

new Fish('Vladimir').greet(); // 'Hello, my name is Vladimir the fish.'

API

defineMethod(constructor, prop, method)

Param Type Description
constructor function The constructor function whose prototype the method will be added to
prop string The property name of the method
method function The method to add

License

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

Package Sidebar

Install

npm i define-method

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • akim-mcmath