javascript
String.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;
javascript
String.prototype.startswith = function(str) { return this.indexOf(str) === 0; };
因为str.lenth
是undefined