Javascript 在 IE 11 中添加 startsWith
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36213455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add startsWith in IE 11
提问by Sandah Aung
回答by jfriend00
Straight from the MDN page, here's the polyfill:
直接从MDN 页面,这里是 polyfill:
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, rawPos) {
var pos = rawPos > 0 ? rawPos|0 : 0;
return this.substring(pos, pos + search.length) === search;
}
});
}
This is safe to use in any browser. If the method already exists, this code will see that and do nothing. If the method does not exist, it will add it to the String prototype so it is available on all strings.
这在任何浏览器中都可以安全使用。如果该方法已经存在,则此代码将看到该方法并且什么都不做。如果该方法不存在,它会将其添加到 String 原型中,以便它可用于所有字符串。
You just add this to one of your JS files in some place where it executes at startup and before you attempt to use .startsWith()
.
您只需将它添加到您的一个 JS 文件中的某个位置,该文件在启动时和您尝试使用.startsWith()
.
回答by tk_
Found an easier way to fix this,
找到了一个更简单的方法来解决这个问题,
function startsWith(str, word) {
return str.lastIndexOf(word, 0) === 0;
}
like wise to find the endswith use below code,
像使用下面的代码一样明智地找到结尾,
function endsWith(str, word) {
return str.indexOf(word, str.length - word.length) !== -1;
}
回答by NuclearPeon
I've been using this polyfill:
我一直在使用这个 polyfill:
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function(prefix) {
return this.slice(0, prefix.length) == prefix;
};
}
回答by fatih
I've reviewed and edited my answer. I believe that my code is now much more appropriate than before.
我已经查看并编辑了我的答案。我相信我的代码现在比以前更合适。
If your browser supports built-in 'startsWith' function then that built-in function will be use. Additionally I've included 'endsWith' also.
如果您的浏览器支持内置的 'startsWith' 函数,那么将使用该内置函数。此外,我还包括了“endsWith”。
if (typeof String.prototype.startsWith !== 'function') {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.substring(position, position + searchString.length) === searchString;
}
}
if (typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function(searchString, strtLength) {
strtLength = (strtLength === undefined || strtLength > this.length)? this.length : strtLength;
return this.substring(strtLength - searchString.length, strtLength) === searchString;
}
}
/* usage */
let str = 'To be, or not to be, that is the question.'
console.log(str.startsWith('To be')) // true
console.log(str.startsWith('not to be')) // false
console.log(str.startsWith('not to be', 10)) // true
let str2 = 'To be, or not to be, that is the question.'
console.log(str2.endsWith('question.')) // true
console.log(str2.endsWith('to be')) // false
console.log(str2.endsWith('to be', 19)) // true