2019-04-21 11:50:48 +08:00

148 lines
6.0 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div>
<div>
<p></p><p></p>
<h2 id="概述">概述</h2>
</div>
</div>
<p><code><strong>startsWith()</strong></code>方法用来判断当前字符串是否是以另外一个给定的子字符串“开头”的,根据判断结果返回 <code>true</code><code>false</code></p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code class="brush:js;"><em>str</em>.startsWith(<em>searchString</em> [, <em>position</em>])</code>;</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>searchString</code></dt>
<dd>要搜索的子字符串。</dd>
<dt><code>position</code></dt>
<dd><code>str</code> 中搜索 <code><var>searchString</var></code> 的开始位置,默认值为 0也就是真正的字符串开头处。</dd>
</dl>
<h2 id="Examples" name="Examples">示例</h2>
<pre><code class="language-js;">var str = "To be, or not to be, that is the question.";
alert(str.startsWith("To be")); // true
alert(str.startsWith("not to be")); // false
alert(str.startsWith("not to be", 10)); // true</code></pre>
<h2 id="Polyfill">Polyfill</h2>
<pre><code class="language-javascript">/*! http://mths.be/startswith v0.2.0 by @mathias */
if (!String.prototype.startsWith) {
(function() {
'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
var defineProperty = (function() {
// IE 8 only supports `Object.defineProperty` on DOM elements
try {
var object = {};
var $defineProperty = Object.defineProperty;
var result = $defineProperty(object, object, object) &amp;&amp; $defineProperty;
} catch(error) {}
return result;
}());
var toString = {}.toString;
var startsWith = function(search) {
if (this == null) {
throw TypeError();
}
var string = String(this);
if (search &amp;&amp; toString.call(search) == '[object RegExp]') {
throw TypeError();
}
var stringLength = string.length;
var searchString = String(search);
var searchLength = searchString.length;
var position = arguments.length &gt; 1 ? arguments[1] : undefined;
// `ToInteger`
var pos = position ? Number(position) : 0;
if (pos != pos) { // better `isNaN`
pos = 0;
}
var start = Math.min(Math.max(pos, 0), stringLength);
// Avoid the `indexOf` call if no match is possible
if (searchLength + start &gt; stringLength) {
return false;
}
var index = -1;
while (++index &lt; searchLength) {
if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
return false;
}
}
return true;
};
if (defineProperty) {
defineProperty(String.prototype, 'startsWith', {
'value': startsWith,
'configurable': true,
'writable': true
});
} else {
String.prototype.startsWith = startsWith;
}
}());
}</code></pre>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Edge</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>41</td>
<td><a href="/en-US/Firefox/Releases/17" title="Released on 2012-11-20.">17</a> (17)</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>28</td>
<td>9</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td>36</td>
<td>17.0 (17)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
<p> </p>
<p>请注意尽管MSDN文档有清晰地指示这个方法 (https://msdn.microsoft.com/en-us/library/mt146831(v=vs.94).aspx) 不支持Internet Explorer,但是这个方法似乎会在很久之前的Internet Explorer 8下生效。</p>
</div>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/String/endsWith" title="endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。"><code>String.prototype.endsWith()</code></a> <span title="这是一个实验性的 API请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></li>
<li><a href="Reference/Global_Objects/String/contains" title="此页面仍未被本地化, 期待您的翻译!"><code>String.prototype.contains()</code></a> <span title="这是一个实验性的 API请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></li>
<li><a href="Reference/Global_Objects/String/indexOf" title="indexOf() 方法返回调用  String 对象中第一次出现的指定值的索引开始在 fromIndex进行搜索。"><code>String.prototype.indexOf()</code></a></li>
</ul>
</article>