I've looked in ECMAScript 2015 recently.
Does ECMAScript 2015 have class initializer?
For example, I tried to write class like a parser;
class URLParser {
parse(url) {
let regex = /(https?):\/\/([^\/]+)\/([^\?]*)\?([^#]*)#(.*)/;
(....)
}
}
var a = new URLParser();
a.parse('http://example.com/abc');
a.parse('http://example.com/def');
var b = new URLParser();
b.parse('https://sample.net/abc');
b.parse('https://sample.net/def');
This regex is common in class, so I'd like to initialize it only once.
I know to use constructor for reduction of initializing, but affects instance wide.
I'd like to know how to reduct initializing class wide.
Thank you.
Nope. There is a proposal for static properties though.
Until then, as always, you can add shared properties to the prototype:
URLParser.prototype.regex = /(https?):\/\/([^\/]+)\/([^\?]*)\?([^#]*)#(.*)/;
and access it inside your method via this.regex
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With