From d678596223044ea0cf2f52a29e085debdeda9dcd Mon Sep 17 00:00:00 2001 From: Jason Lam Date: Thu, 10 Aug 2023 14:05:30 +0800 Subject: [PATCH] [zh-cn]: Update translation of String.startsWith() (#15020) Co-authored-by: A1lo --- .../global_objects/string/startswith/index.md | 46 ++++++++----------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.md b/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.md index aabfefb32dc9c3..88224d334fcfb8 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.md +++ b/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.md @@ -5,58 +5,47 @@ slug: Web/JavaScript/Reference/Global_Objects/String/startsWith {{JSRef}} -**`startsWith()`** 方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 `true` 或 `false`。 +{{jsxref("String")}} 的 **`startsWith()`** 方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 `true` 或 `false`。 {{EmbedInteractiveExample("pages/js/string-startswith.html")}} ## 语法 -```plain -str.startsWith(searchString[, position]) +```js-nolint +startsWith(searchString) +startsWith(searchString, position) ``` ### 参数 - `searchString` - - : 要搜索的子字符串。 + - : 要在该字符串开头搜索的子串。不能是[正则表达式](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp#正则表达式的特殊处理)。所有不是正则表达式的值都会被[强制转换为字符串](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#字符串强制转换),因此省略它或传递 `undefined` 将导致 `startsWith()` 搜索字符串 `"undefined"`,这应该不是你想要的结果。 - `position` {{optional_inline}} - - : 在 `str` 中搜索 `searchString` 的开始位置,默认值为 0。 + - : `searchString` 期望被找到的起始位置(即 `searchString` 的第一个字符的索引)。默认为 `0`。 ### 返回值 -如果在字符串的开头找到了给定的字符则返回 **`true`**;否则返回 **`false`**。 +如果给定的字符在字符串的开头被找到(包括当 `searchString` 是空字符串时),则返回 **`true`**;否则返回 **`false`**。 -## 描述 - -这个方法能够让你确定一个字符串是否以另一个字符串开头。这个方法区分大小写。 - -## Polyfill +### 异常 -此方法已被添加至 ECMAScript 2015 规范之中,但可能不能在所有的现行 JavaScript 实现中使用。不过,你可以用以下的代码段为 `String.prototype.startsWith()` 制作 Polyfill: +- {{jsxref("TypeError")}} + - : 如果 `searchString` 是[正则表达式](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp#正则表达式的特殊处理),则抛出该异常。 -```js -if (!String.prototype.startsWith) { - Object.defineProperty(String.prototype, "startsWith", { - value: function (search, pos) { - pos = !pos || pos < 0 ? 0 : +pos; - return this.substring(pos, pos + search.length) === search; - }, - }); -} -``` +## 描述 -Mathias Bynens 在 Github 上提供了[一份更为稳定有效(完全符合 ES2015 规范),但性能略差、代码紧凑性微减的 PolyFill](https://github.com/mathiasbynens/String.prototype.startsWith)。 +这个方法能够让你确定一个字符串是否以另一个字符串开头。这个方法区分大小写。 ## 示例 -### 使用 `startsWith()` +### 使用 startsWith() ```js -var str = "To be, or not to be, that is the question."; +const 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 +console.log(str.startsWith("To be")); // true +console.log(str.startsWith("not to be")); // false +console.log(str.startsWith("not to be", 10)); // true ``` ## 规范 @@ -69,6 +58,7 @@ alert(str.startsWith("not to be", 10)); // true ## 参见 +- [`core-js` 中 `String.prototype.startsWith` 的 polyfill](https://github.com/zloirock/core-js#ecmascript-string-and-regexp) - {{jsxref("String.prototype.endsWith()")}} - {{jsxref("String.prototype.includes()")}} - {{jsxref("String.prototype.indexOf()")}}