https://github.com/drsensor/optnew
Make `new` keyword optional when instantiating ES6 class. Just like in Dart 😉
https://github.com/drsensor/optnew
Last synced: over 1 year ago
JSON representation
Make `new` keyword optional when instantiating ES6 class. Just like in Dart 😉
- Host: GitHub
- URL: https://github.com/drsensor/optnew
- Owner: DrSensor
- License: mit
- Created: 2020-12-10T00:36:00.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-10T00:37:24.000Z (over 5 years ago)
- Last Synced: 2025-03-10T19:57:17.579Z (over 1 year ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `optnew`
Make `new` keyword optional when instantiating es6-class. Just like in Dart 😉
## Usage
Given this class
```js
// 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
```js
import optnew from "optnew"
@optnew
class D {
public
static static
}
@optnew
class C extends ToBeInherited {}
```
results:
```js
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
```js
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:
```js
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"
```