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

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -