javascript - String.prototype.myFunction not returning a string? -
why below code not return this properly? should return 'image', rather of letters in object, shouldn't it?
string.prototype.removeextension = function(){ return (this.lastindexof('.') !== -1) ? this.substr(0, this.lastindexof('.')) : this; } 'image.jpg'.removeextension(); // returns 'image' 'image.png.jpg'.removeextension(); // returns 'image.jpg' 'image'.removeextension(); // returns string {0: "i", 1: "m", 2: "a", 3: "g", 4: "e", removeextension: function}
this
references object within context of scope (*). need invoke .tostring()
instance, pseudo primitive value out of it.
return this.tostring();
if return this
that, it'll reference current instance of string-object
invoked.
(*) (only exception es5 strict mode, this
might reference undefined
value
Comments
Post a Comment