# old

> Make the 'new' keyword optional for ES6 classes

Latest version **0.2.0** (published 2017-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install old
pnpm add old
yarn add old
bun add old
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2017-01-25 |
| First published | 2016-05-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Matt Bell |
| Maintainers | mappum |
| Keywords | new, es6, classes, optional, keyword |

## Links

- npm: https://www.npmjs.com/package/old
- Repository: https://github.com/mappum/no-new
- Homepage: https://github.com/mappum/no-new#readme
- Issues: https://github.com/mappum/no-new/issues
- npm.io page: https://npm.io/package/old

## Dependencies (1)

- [object-assign](https://npm.io/package/object-assign.md) ^4.1.0

## Recent versions

- 0.2.0 (latest) — 2017-01-25
- 0.1.3 — 2016-05-20
- 0.1.2 — 2016-05-02
- 0.1.1 — 2016-05-02

## README

# old

[![npm version](https://img.shields.io/npm/v/old.svg)](https://www.npmjs.com/package/old)

**Make the 'new' keyword optional for ES6 classes**

## Usage

`npm install old`

```js
var old = require('old')

class Class {
  // ...
}

var Class2 = old(Class)

Class() // throws error
Class2() // creates an instance of Class
```

## Rationale

With ES5 "constructor" functions, a common pattern is to make the `new` keyword optional by doing something like the following:
```js
function Foo () {
  if (!(this instanceof Foo)) return new Foo()
  // do constructor stuff
}
```

Recently, ES6 introduced classes to replace constructor functions. However, if these classes are instantiated without `new`, an error is thrown: `TypeError: Class constructor Foo cannot be invoked without 'new'`. This module makes `new` optional, even for these ES6 classes.

## Credit

- Thank you to Sorella in ##javascript (Freenode) for the clean solution
- Thanks to slikts in ##javascript for the name idea

---
_Source: https://npm.io/package/old · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
