Skip to content

mcmath/define-static-method

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Define Static Method

Version License Build Coverage Dependencies

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

Install

Install with npm:

npm install --save define-static-method

Usage

In ES2015, we might do this:

class Squid {
  constructor(name) {
    this.name = name;
  }
  static createVladimir() {
    return new Squid('Vladimir');
  }
}

In ES5, we can do this:

var defineStatic = require('define-static-method');

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

defineStatic(Squid, 'createVladimir', function() {
  return new Squid('Vladimir');
});

And here is Vladimir:

Squid.createVladimir().name; // -> 'Vladimir'

API

defineStaticMethod(constructor, prop, method)

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

License

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

About

Define an ES2015 class static method in ES5

Resources

License

Stars

Watchers

Forks

Packages

No packages published