I recently looked at ECMAScript 2015.
Is there a class initializer in ECMAScript 2015?
For example, I tried to write a class as 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 regular expression is common in the class , so I would like to initialize it only once.
I know that to use the constructor to reduce initialization, but it affects the instance wide .
I would like to know how to restore class initialization .
Thank.
source
share