Allow calling without new constructor
bitinn opened this issue · comments
I mean adding something like:
// allow call as function
if (!(this instanceof Path))
return new Path(path);
I guess it comes down to personal preference, having function-style call support make using path-parser
directly easier (which I am doing at the moment, having to write a bunch of new Path
just seem a bit weird).
Hi @bitinn, I'll look into this!
Because I am using ES6 classes, the transpiler (babel) won't allow calling the class without new
(which makes sense: the day you stop transpiling and start running native ES6 💣...).
I don't see a non-hackish way of doing this. I could have a static function on the class which would call its constructor.
Maybe a static create function as default export?!
export class Path {
static create(path) {
return new Path(path);
}
}
export default Path.create;
Could be used as Constructor…
import { Path } from 'path-parser';
var path = new Path('/test/:foo');
…or as function by using the default export:
import createPath from 'path-parser';
var path = createPath('/test/:foo');
👍 Yes, I have it but didn't commit it yet.
Fixed for now, I didn't change the export to avoid introducing a breaking change.
export default class Path {
static createPath(path) {
return new Path(path);
}
/* ... */
}