javascript - Extend a String class in es6 -
i can write following in es5:
string.prototype.something=function(){ return this.split(' ').join(''); };
how do same thing in es6 using new features ?
ps : know valid es6. wan't know whether there's other way of implementing such functions in es6 shorter ? above function example.
in es6 can object.assign()
this:
object.assign(string.prototype, { something() { return this.split(' ').join(); } });
you can find more info method here.
or use defineproperty
(i think better here):
object.defineproperty(string.prototype, 'something', { value() { return this.split(' ').join(); } });
see docs here.
see comment see when use defineproperty
vs object.assign()
.
Comments
Post a Comment