【发布时间】:2013-04-01 12:57:31
【问题描述】:
为什么下面的代码没有正确返回 this?它应该只返回“图像”,而不是对象中的所有字母,不是吗?
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}
【问题讨论】:
-
jsfiddle.net/Allendar/38Yu9 (
this.toString())
标签: javascript string this function-prototypes