├── .gitignore ├── .npmignore ├── .npmrc ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .nyc_output 3 | v8.log 4 | coverage/ 5 | node_modules/ 6 | cjs/* 7 | !cjs/package.json 8 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .nyc_output 3 | .eslintrc.json 4 | .github/ 5 | .travis.yml 6 | v8.log 7 | coverage/ 8 | node_modules/ 9 | rollup/ 10 | test/ 11 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @ungap/new 2 | 3 | This is a resolution to this [TC39 proposal](https://es.discourse.group/t/function-prototype-new/1772): 4 | 5 | * if they implement the proposal, this is exactly it 6 | * if they won't implement the proposal, this is not causing any present/past/future conflict or issue with already working code in the wild (i.e. static `new` classes methods won't be affected and `new` object fields won't be affected neither) 7 | 8 | #### Alternative 9 | 10 | If you don't like the idea to simplify everyone life with this module, feel free to try [this workaround](https://github.com/WebReflection/proxy-class-new#readme) that doesn't change the `Function.prototype` at all. 11 | 12 | ## Use Case 13 | 14 | Specially in these days where many foreign programming languages are landing in the Web landscape via WASM, it is crucial to provide an easy way to disambiguate users intents when it comes to `new Class(...args)`, a syntax that *might not be available* in foreign programming languages. 15 | 16 | This extremely tiny and TC39 suggested polyfill allows code interpreted in these programming language to use `Class.new(...args)` instead, avoiding the need to workaround or patch their parser and/or implement on their own a solution for this very common problem. 17 | 18 | ## Usage 19 | 20 | ```js 21 | // these all work as expected in any client/server project 22 | import '@ungap/new'; 23 | require('@ungap/new'); 24 | importScripts('https://unpkg.com/@ungap/new'); 25 | 26 | Date.new(0) instanceof Date; // true 27 | // same as new Date(0) 28 | ``` 29 | 30 | Happy WASM <-> JS coding 🥳 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | (({prototype:p},n)=>{if(!(n in p)){const{construct:c}=Reflect;Object.defineProperty(p,n,{configurable:!0,value(){return c(this,arguments)}})}})(Function,'new'); 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@ungap/new", 3 | "version": "0.1.1", 4 | "description": "An extension to allow `Class.new(...args)` instead of `new Class(...args)`", 5 | "main": "./index.js", 6 | "scripts": { 7 | "test": "node test/index.js" 8 | }, 9 | "keywords": [ 10 | "function", 11 | "class", 12 | "new" 13 | ], 14 | "author": "Andrea Giammarchi", 15 | "license": "ISC", 16 | "module": "./esm/index.js" 17 | } 18 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | require('../index.js'); 2 | 3 | console.assert(Date.new(0) instanceof Date); 4 | 5 | let constructTrap = false; 6 | let result = (new Proxy(Date, { 7 | construct(target, args) { 8 | constructTrap = true; 9 | return new target(...args); 10 | } 11 | })).new(); 12 | 13 | console.assert(constructTrap); 14 | console.assert(result instanceof Date); 15 | --------------------------------------------------------------------------------