https://github.com/serapath/fnewless
turn a function with an object or function as it's prototype into a constructor you CAN call without new
https://github.com/serapath/fnewless
Last synced: about 1 year ago
JSON representation
turn a function with an object or function as it's prototype into a constructor you CAN call without new
- Host: GitHub
- URL: https://github.com/serapath/fnewless
- Owner: serapath
- License: mit
- Created: 2018-02-02T19:49:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-05T11:19:51.000Z (over 8 years ago)
- Last Synced: 2025-03-15T14:03:53.632Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/fnewless
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fnewless
turn a function with an object or function as it's prototype into a constructor you CAN call without new
# usage
```js
var Constructor = require('fnewless')
/******************************************************************************
make object instances
******************************************************************************/
// make newless constructor
function CTOR_obj (x) { this.x = x }
CTOR_obj.prototype.type = 'obj'
var ctor1 = Constructor(CTOR_obj)
// make instance
var obj = ctor1('object') // or `new ctor1('object')`
// test
console.log(obj instanceof ctor1) // true
console.log(obj.constructor === ctor1) // true
console.log(obj.x) // 'object'
console.log(obj.type) // 'obj'
/******************************************************************************
make function instances
******************************************************************************/
// make newless constructor
function CTOR_fn (x) { this.x = x }
CTOR_fn.prototype = function () { return this.x.toUpperCase() }
CTOR_fn.prototype.type = 'fn'
var ctor2 = Constructor(CTOR_fn)
// make instance
var fn = ctor2('function') // or `new ctor2('function')`
// test
console.log(fn instanceof ctor2) // true
console.log(fn.constructor === ctor2) // true
console.log(fn.x) // 'function'
console.log(fn.type) // 'fn'
console.log(fn()) // 'FUNCTION'
```