Socket
Socket
Sign inDemoInstall

@activeprospect/old

Package Overview
Dependencies
1
Maintainers
20
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @activeprospect/old

Make the 'new' keyword optional for ES6 classes


Version published
Weekly downloads
131
decreased by-2.96%
Maintainers
20
Install size
10.5 kB
Created
Weekly downloads
 

Readme

Source

old

npm version

Make the 'new' keyword optional for ES6 classes

Usage

npm install old

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:

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

Keywords

FAQs

Last updated on 30 Apr 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc