New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

optnew

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

optnew

Make `new` keyword optional when instantiate class. Just like in Dart 😉

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

optnew

Make new keyword optional when instantiating es6-class. Just like in Dart 😉

Usage

Given this class

// all possible ES class features
class ToBeInherited {
  #private = crypto.getRandomValues(new Uint8Array(1))[0]

  public = 2

  get private() { return this.#private }

  static static = 'zap'

  constructor(arg1, arg2) {
    this.oldstyle = `${this.#private}-${arg1},${arg2}`
  }

  [Symbol("private")] = crypto.getRandomValues(new Uint32Array(1))[0]

  static [Symbol("static")] = 10
}

as decorator

import optnew from "optnew"

@optnew
class D {
  public
  static static
}

@optnew
class C extends ToBeInherited {}

results:

console.log(C)            // [Function: C] { static: 'zap', [Symbol("static")]: 10 }
console.log(new C(1, 2))  // C { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }
console.log(C(1, 2))      // C { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }

console.log(D)      // [Function: D] { static: undefined }
console.log(new D)  // D { public: undefined }
console.log(D())    // D { public: undefined }

as function

import optnew from "optnew"

const A = optnew(class {
  public
  static static
})

const AI = optnew(class extends ToBeInherited {})

class C extends ToBeInherited {}
C = optnew(C)

results:

console.log(A)    // [Function: (anonymous)] { static: undefined }
console.log(A)    // (anonymous) { public: undefined }
console.log(A())  // (anonymous) { public: undefined }

console.log(AI)            // [Function: (anonymous)] { static: 'zap', [Symbol("static")]: 10 }
console.log(new AI(1, 2))  // (anonymous) { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }
console.log(AI(1, 2))      // (anonymous) { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }

console.log(C)            // [Function: C] { static: 'zap', [Symbol("static")]: 10 }
console.log(new C(1, 2))  // C { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }
console.log(C(1, 2))      // C { public: 2, oldstyle: '209-1, 2 ', [Symbol(private)]: 1155023846 }

console.log(A.name)   // ""
console.log(AI.name)  // ""
console.log(C.name)   // "C"

Keywords

decorater

FAQs

Package last updated on 10 Dec 2020

Did you know?

Socket

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