disallow-new

Don't let people use "new" on your function

Usage no npm install needed!

<script type="module">
  import disallowNew from 'https://cdn.skypack.dev/disallow-new';
</script>

README

This is a simple utility for disallowing the use of "new" with certain functions. It's especially useful for when you have a capitalized factory function that you don't want people to use as a constructor (which would cause an unnecessary object allocation but otherwise succeed without warning).

Installation

npm install disallow-new

Usage

import disallowNew from 'disallow-new';

function MyFactory() {
  // [SNIP]
}

export default disallowNew(MyFactory);

Elsewhere:

let a = new MyFactory(); // Errors!
let a = MyFactory(); // All good.