首页 > javascript实现startswith

javascript实现startswith

javascriptString.prototype.startswith = function(str) {
    return this.substr(0, str.lenth) === str;
};

验证如下,但是并没有如愿返回true

var s = 'what the fuck';
s.startswith('what')
>false

实现trim(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)方法的时候也是这样进行的,但是为什么这个会出错?


这。。。你不能把startswith该成startsWith么?
chrome里原生自带的,不用自己写。。。--b


return this.substr(0, str.lenth) === str; // 拼写错误
return this.substr(0, str.length) === str;

javascriptString.prototype.startswith = function(str) {
    return this.indexOf(str) === 0;
};

因为str.lenth是undefined

【热门文章】
【热门文章】