javascript实现startswith
高洛峰
高洛峰 2017-04-10 14:57:26
0
3
800
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)方法的时候也是这样进行的,但是为什么这个会出错?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

répondre à tous(3)
巴扎黑
return this.substr(0, str.lenth) === str; // 拼写错误
return this.substr(0, str.length) === str;
左手右手慢动作

因为str.lenth是undefined

PHPzhong

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

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!