From 563ca0a35e98678e2b7d5f154f31f496851e8d60 Mon Sep 17 00:00:00 2001 From: t7yang Date: Mon, 10 Jan 2022 08:38:07 +0800 Subject: remove code tag inside pre tag for zh-CN --- .../reference/global_objects/array/flat/index.html | 12 +++++------- .../reference/global_objects/array/length/index.html | 8 ++++---- .../reference/global_objects/date/getdate/index.html | 6 +++--- .../reference/global_objects/date/getday/index.html | 6 +++--- .../reference/global_objects/date/getfullyear/index.html | 2 +- .../reference/global_objects/date/gethours/index.html | 4 ++-- .../global_objects/date/getmilliseconds/index.html | 4 ++-- .../reference/global_objects/date/getminutes/index.html | 6 +++--- .../reference/global_objects/date/getseconds/index.html | 6 +++--- .../global_objects/date/gettimezoneoffset/index.html | 2 +- .../reference/global_objects/date/parse/index.html | 16 ++++++++-------- .../reference/global_objects/date/setfullyear/index.html | 6 +++--- .../reference/global_objects/infinity/index.html | 4 ++-- .../reference/global_objects/isfinite/index.html | 4 ++-- .../javascript/reference/global_objects/isnan/index.html | 4 ++-- .../reference/global_objects/math/pi/index.html | 4 ++-- .../reference/global_objects/math/random/index.html | 10 +++++----- .../reference/global_objects/math/sign/index.html | 4 ++-- .../object/getownpropertydescriptors/index.html | 4 ++-- .../reference/global_objects/object/valueof/index.html | 4 ++-- .../reference/global_objects/parsefloat/index.html | 8 ++++---- .../reference/global_objects/set/has/index.html | 4 ++-- .../reference/global_objects/string/match/index.html | 16 ++++++++-------- .../reference/global_objects/string/padstart/index.html | 8 ++++---- .../reference/global_objects/string/repeat/index.html | 4 ++-- .../global_objects/string/tolowercase/index.html | 2 +- .../reference/global_objects/symbol/index.html | 16 ++++++++-------- .../reference/global_objects/undefined/index.html | 4 ++-- .../reference/global_objects/webassembly/index.html | 4 ++-- 29 files changed, 90 insertions(+), 92 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/global_objects') diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/flat/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/flat/index.html index f6a9420e21..4f06aff1fc 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/flat/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/flat/index.html @@ -78,16 +78,14 @@ const flattened = arr => [].concat(...arr);

reduce + concat + isArray + recursivity

-
// 使用 reduce、concat 和递归展开无限多层嵌套的数组
-var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
-
-function flatDeep(arr, d = 1) {
+
// 使用 reduce、concat 和递归展开无限多层嵌套的数组
+  var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
+function flatDeep(arr, d = 1) {
    return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
                 : arr.slice();
-};
-
+};
 flatDeep(arr1, Infinity);
-// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
+// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]

forEach+isArray+push+recursivity

diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html index 8f17e9af1f..de5862921d 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html @@ -82,25 +82,25 @@ function printEntries(arr) {

下面的例子中,通过数组下标遍历数组元素,并把每个元素的值修改为原值的2倍。

-
var numbers = [1, 2, 3, 4, 5];
+
var numbers = [1, 2, 3, 4, 5];
 var length = numbers.length;
 for (var i = 0; i < length; i++) {
   numbers[i] *= 2;
 }
-// 遍历后的结果 [2, 4, 6, 8, 10]
+// 遍历后的结果 [2, 4, 6, 8, 10]

截断数组

下面的例子中,如果数组长度大于 3,则把该数组的长度截断为 3 。

-
var numbers = [1, 2, 3, 4, 5];
+
var numbers = [1, 2, 3, 4, 5];
 
 if (numbers.length > 3) {
   numbers.length = 3;
 }
 
 console.log(numbers); // [1, 2, 3]
-console.log(numbers.length); // 3
+console.log(numbers.length); // 3

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getdate/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getdate/index.html index 5f7d438415..57f12998f6 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/getdate/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getdate/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDate

语法

-
dateObj.getDate()
+
dateObj.getDate()

参数

@@ -29,10 +29,10 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDate

下面第二条语句将值25赋给 day 变量,该值基于日期对象 Xmax95的值。

-
var Xmas95 = new Date("December 25, 1995 23:15:00");
+
var Xmas95 = new Date("December 25, 1995 23:15:00");
 var day = Xmas95.getDate();
 
-alert(day); // 25
+alert(day); // 25

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html index b2ee4a80cd..584180a8fb 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDay

语法

-
dateObj.getDay()
+
dateObj.getDay()
 

返回值

@@ -26,10 +26,10 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDay

下面第二条语句,基于{{jsxref("Date")}}对象 Xmas95 的值,把 1 赋值给 weekday。也就是说1995年12月25日是星期一。

-
var Xmas95 = new Date("December 25, 1995 23:15:30");
+
var Xmas95 = new Date("December 25, 1995 23:15:30");
 var weekday = Xmas95.getDay();
 
-console.log(weekday); // 1
+console.log(weekday); // 1

注意:如果需要,可以使用{{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}与一个额外的options 参数,从而返回这天的全称(如"Monday").使用此方法,结果会更加国际化:

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getfullyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getfullyear/index.html index 6b8638c52d..c29a10aa45 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/getfullyear/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getfullyear/index.html @@ -21,7 +21,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getFullYear

语法

-
dateObj.getFullYear()
+
dateObj.getFullYear()
 

返回值

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/gethours/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/gethours/index.html index fda9396354..62d35dd713 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/gethours/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/gethours/index.html @@ -29,10 +29,10 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getHours

下面第二条语句,基于日期对象 Xmas95 的值,把 23 赋值给了变量 hours。

-
var Xmas95 = new Date("December 25, 1995 23:15:00");
+
var Xmas95 = new Date("December 25, 1995 23:15:00");
 var hours = Xmas95.getHours();
 
-alert(hours); // 23
+alert(hours); // 23

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getmilliseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getmilliseconds/index.html index c5dd09e3d9..2cbc0b9d30 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/getmilliseconds/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getmilliseconds/index.html @@ -29,9 +29,9 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getMilliseconds

下例中,将当前时间的毫秒数赋值给变量 ms

-
var ms;
+
var ms;
 Today = new Date();
-ms = Today.getMilliseconds();
+ms = Today.getMilliseconds();

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getminutes/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getminutes/index.html index 8322a9e813..a27f4c265d 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/getminutes/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getminutes/index.html @@ -11,7 +11,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getMinutes

语法

-
dateObj.getMinutes()
+
dateObj.getMinutes()

参数

@@ -27,8 +27,8 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getMinutes

下例中,第二行语句运行过后,变量 minutes 的值为15,也就是说 Xmas95 这个日期对象的值为某时15分某秒。

-
var Xmas95 = new Date("December 25, 1995 23:15:00");
-var minutes = Xmas95.getMinutes();
+
var Xmas95 = new Date("December 25, 1995 23:15:00");
+var minutes = Xmas95.getMinutes();

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getseconds/index.html index ded6203208..1cb80b2ea8 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/getseconds/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getseconds/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getSeconds

语法

-
dateObj.getSeconds()
+
dateObj.getSeconds()

参数

@@ -29,8 +29,8 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getSeconds

下面第二条语句,基于日期对象 Xmas95 的值,把 30 赋值给变量 secs

-
var Xmas95 = new Date("December 25, 1995 23:15:30");
-var secs = Xmas95.getSeconds();
+
var Xmas95 = new Date("December 25, 1995 23:15:30");
+var secs = Xmas95.getSeconds();
 
diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/gettimezoneoffset/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/gettimezoneoffset/index.html index 7ee056a7df..90b7d1a962 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/gettimezoneoffset/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/gettimezoneoffset/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

语法

-
dateObj.getTimezoneOffset()
+
dateObj.getTimezoneOffset()

参数

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html index 68a8d136e7..5b0a6cf01d 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html @@ -70,26 +70,26 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/parse

但是, 在如 ECMA-262 规范中定义的情况,如果因为无效值而导致日期字符串不能被识别为 ISO 格式时,根据浏览器和给定的值不同,返回值可以是,也可以不是 {{jsxref("NaN")}} 。比如:

-
// 包含无效值的非 ISO 格式字符串
-new Date('23/25/2014');
+
// 包含无效值的非 ISO 格式字符串
+new Date('23/25/2014');

在 Firefox 30 中会被识别为本地时区的2015年12月25日,而在 Safari 7 中则是无效日期。但是,如果字符串被识别为 ISO 格式并且包含无效值,则在所有遵循 ES5 或者更新标准的浏览器中都会返回 {{jsxref("NaN")}} 。

-
// 包含无效值的 ISO 格式字符串
+
// 包含无效值的 ISO 格式字符串
 new Date('2014-25-23').toISOString();
-// 在所有遵循 ES5的浏览器中返回 "RangeError: invalid date"
+// 在所有遵循 ES5的浏览器中返回 "RangeError: invalid date"

SpiderMonkey 的引擎策略可以在 jsdate.cpp  中找到。字符串 "10 06 2014"  可以作为非 ISO 格式字符串使用自定义处理方式的例子。参见这篇关于解析如何进行的粗略纲要

-
new Date('10 06 2014');
+
new Date('10 06 2014');

将会被解析为本地时间 2014年10月6日,而不是6月10日。另一个例子

-
new Date('foo-bar 2014').toString();
+
new Date('foo-bar 2014').toString();
 // 返回: "Invalid Date"
 
 Date.parse('foo-bar 2014');
-// 返回: NaN
+// 返回: NaN

例子

@@ -97,7 +97,7 @@ Date.parse('foo-bar 2014');

如果 IPOdate 是一个已经存在的 {{jsxref("Date")}} 对象,则可以把其设置为本地时间 1995年8月9日。如下:

-
IPOdate.setTime(Date.parse('Aug 9, 1995'));
+
IPOdate.setTime(Date.parse('Aug 9, 1995'));

其他一些解析非标准格式日期的例子:

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setfullyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setfullyear/index.html index c8fcb530ab..e5693e9e07 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/date/setfullyear/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setfullyear/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/setFullYear

语法

-
dateObj.setFullYear(yearValue[, monthValue[, dayValue]])
+
dateObj.setFullYear(yearValue[, monthValue[, dayValue]])

参数

@@ -36,8 +36,8 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Date/setFullYear

例子:使用setFullYear方法

-
var theBigDay = new Date();
-theBigDay.setFullYear(1997);
+
var theBigDay = new Date();
+theBigDay.setFullYear(1997);

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/infinity/index.html b/files/zh-cn/web/javascript/reference/global_objects/infinity/index.html index f20755f907..929985516d 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/infinity/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/infinity/index.html @@ -25,11 +25,11 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Infinity

示例

-
console.log(Infinity          ); /* Infinity */
+
console.log(Infinity          ); /* Infinity */
 console.log(Infinity + 1      ); /* Infinity */
 console.log(Math.pow(10, 1000)); /* Infinity */
 console.log(Math.log(0)       ); /* -Infinity */
-console.log(1 / Infinity      ); /* 0 */
+console.log(1 / Infinity ); /* 0 */

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/isfinite/index.html b/files/zh-cn/web/javascript/reference/global_objects/isfinite/index.html index e69904ba41..cea70e68ee 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/isfinite/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/isfinite/index.html @@ -33,7 +33,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/isFinite

示例

-
isFinite(Infinity);  // false
+
isFinite(Infinity);  // false
 isFinite(NaN);       // false
 isFinite(-Infinity); // false
 
@@ -41,7 +41,7 @@ isFinite(0);         // true
 isFinite(2e64);      // true, 在更强壮的Number.isFinite(null)中将会得到false
 
 
-isFinite("0");       // true, 在更强壮的Number.isFinite('0')中将会得到false
+isFinite("0"); // true, 在更强壮的Number.isFinite('0')中将会得到false
 
diff --git a/files/zh-cn/web/javascript/reference/global_objects/isnan/index.html b/files/zh-cn/web/javascript/reference/global_objects/isnan/index.html index fac7d74714..1e23c1d0e4 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/isnan/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/isnan/index.html @@ -48,10 +48,10 @@ translation_of: Web/JavaScript/Reference/Global_Objects/isNaN

一个isNaN的 polyfill 可以理解为(这个polyfill利用了NaN自身永不相等于自身这一特征 ):

-
var isNaN = function(value) {
+
var isNaN = function(value) {
     var n = Number(value);
     return n !== n;
-};
+};

示例

diff --git a/files/zh-cn/web/javascript/reference/global_objects/math/pi/index.html b/files/zh-cn/web/javascript/reference/global_objects/math/pi/index.html index fcfc82fbd5..efc463d5b7 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/math/pi/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/math/pi/index.html @@ -23,11 +23,11 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/PI

下面的函数使用 Math.PI 计算给定半径的圆周长:

-
function calculateCircumference (radius) {
+
function calculateCircumference (radius) {
   return 2 * Math.PI * radius;
 }
 
-calculateCircumference(1);  // 6.283185307179586
+calculateCircumference(1); // 6.283185307179586

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/math/random/index.html b/files/zh-cn/web/javascript/reference/global_objects/math/random/index.html index 91796ae506..04602b2699 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/math/random/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/math/random/index.html @@ -50,11 +50,11 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random

这个例子返回了一个在指定值之间的随机整数。这个值不小于 min (如果 min 不是整数,则不小于 min 的向上取整数),且小于(不等于)max

-
function getRandomInt(min, max) {
+
function getRandomInt(min, max) {
   min = Math.ceil(min);
   max = Math.floor(max);
-  return Math.floor(Math.random() * (max - min)) + min; //不含最大值,含最小值
-}
+ return Math.floor(Math.random() * (max - min)) + min; //不含最大值,含最小值 +}

也许很容易想到用 Math.round() 来实现,但是这会导致你的随机数处于一个不均匀的分布,这可能不符合你的需求。

@@ -64,11 +64,11 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random

上一个例子提到的函数 getRandomInt() 结果范围包含了最小值,但不含最大值。如果你的随机结果需要同时包含最小值和最大值,怎么办呢?  getRandomIntInclusive() 函数可以实现。

-
function getRandomIntInclusive(min, max) {
+
function getRandomIntInclusive(min, max) {
   min = Math.ceil(min);
   max = Math.floor(max);
   return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 
-}
+}

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/math/sign/index.html b/files/zh-cn/web/javascript/reference/global_objects/math/sign/index.html index 60808dde44..590089e3c1 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/math/sign/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/math/sign/index.html @@ -50,12 +50,12 @@ Math.sign(); // NaN

Polyfill

-
function sign(x) {
+
function sign(x) {
     x = +x ;// convert to a number
     if (x === 0 || isNaN(x))
         return x;
     return x > 0 ? 1 : -1;
-}
+}

 

diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertydescriptors/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertydescriptors/index.html index cc321fa788..22df48a042 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertydescriptors/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertydescriptors/index.html @@ -41,14 +41,14 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDes

创建子类的典型方法是定义子类,将其原型设置为超类的实例,然后在该实例上定义属性。这么写很不优雅,特别是对于 getters 和 setter 而言。 相反,您可以使用此代码设置原型:

-
function superclass() {}
+
function superclass() {}
 superclass.prototype = {
   // 在这里定义方法和属性
 };
 function subclass() {}
 subclass.prototype = Object.create(superclass.prototype, Object.getOwnPropertyDescriptors({
   // 在这里定义方法和属性
-}));
+}));

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/valueof/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/valueof/index.html index e2627097bd..82d2bc61d9 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/object/valueof/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/object/valueof/index.html @@ -147,7 +147,7 @@ console.log( str2.valueOf() === str2 ); // false

改写 .prototype.valueof

-
function MyNumberType(n) {
+
function MyNumberType(n) {
     this.number = n;
 }
 
@@ -156,7 +156,7 @@ MyNumberType.prototype.valueOf = function() {
 };
 
 var myObj = new MyNumberType(4);
-myObj + 3; // 7
+myObj + 3; // 7

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html b/files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html index 21d4e0bfbd..f32b3bbf90 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html @@ -55,13 +55,13 @@ translation_of: Web/JavaScript/Reference/Global_Objects/parseFloat

下面的例子都返回3.14

-
parseFloat(3.14);
+
parseFloat(3.14);
 parseFloat('3.14');
 parseFloat('  3.14  ');
 parseFloat('314e-2');
 parseFloat('0.0314E+2');
 parseFloat('3.14some non-digit characters');
-parseFloat({ toString: function() { return "3.14" } });
+parseFloat({ toString: function() { return "3.14" } });

parseFloat返回NaN

@@ -74,8 +74,8 @@ parseFloat({ toString: function() { return "3.14" } });

以下例子均返回 900719925474099300,当整数太大以至于不能被转换时将失去精度。

-
parseFloat(900719925474099267n);
-parseFloat('900719925474099267n');
+
parseFloat(900719925474099267n);
+parseFloat('900719925474099267n');

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/has/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/has/index.html index 0bbad115ec..47369b1f27 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/set/has/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/set/has/index.html @@ -35,7 +35,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Set/has

使用 has 方法

-
var mySet = new Set();
+
var mySet = new Set();
 mySet.add('foo');
 
 mySet.has('foo');  // 返回 true
@@ -47,7 +47,7 @@ set1.add(obj1);
 
 set1.has(obj1);        // 返回 true
 set1.has({'key1': 1}); // 会返回 false,因为其是另一个对象的引用
-set1.add({'key1': 1}); // 现在 set1 中有2条(不同引用的)对象了
+set1.add({'key1': 1}); // 现在 set1 中有2条(不同引用的)对象了

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html index 33a3a57880..f369a7a7f9 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html @@ -60,7 +60,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/match

在下例中,使用 match 查找 "Chapter" 紧跟着 1 个或多个数值字符,再紧跟着一个小数点和数值字符 0 次或多次。正则表达式包含 i 标志,因此大小写会被忽略。

-
var str = 'For more information, see Chapter 3.4.5.1';
+
var str = 'For more information, see Chapter 3.4.5.1';
 var re = /see (chapter \d+(\.\d)*)/i;
 var found = str.match(re);
 
@@ -76,30 +76,30 @@ console.log(found);
 // 'Chapter 3.4.5.1' 被'(chapter \d+(\.\d)*)'捕获。
 // '.1' 是被'(\.\d)'捕获的最后一个值。
 // 'index' 属性(22) 是整个匹配从零开始的索引。
-// 'input' 属性是被解析的原始字符串。
+// 'input' 属性是被解析的原始字符串。

例子:match 使用全局(global)和忽略大小写(ignore case)标志

下例展示了 match 使用 global 和 ignore case 标志。A-E、a-e 的所有字母将会作为一个数组的元素返回。

-
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
+
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
 var regexp = /[A-E]/gi;
 var matches_array = str.match(regexp);
 
 console.log(matches_array);
-// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
+// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

使用match(),不传参数

-
var str = "Nothing will come of nothing.";
+
var str = "Nothing will come of nothing.";
 
-str.match();   // returns [""]
+str.match(); // returns [""]

一个非正则表达式对象作为参数

当参数是一个字符串或一个数字,它会使用new RegExp(obj)来隐式转换成一个 {{jsxref("RegExp")}}。如果它是一个有正号的正数,RegExp() 方法将忽略正号。

-
var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
+
var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
     str2 = "My grandfather is 65 years old and My grandmother is 63 years old.",
     str3 = "The contract was declared null and void.";
 str1.match("number");   // "number" 是字符串。返回["number"]
@@ -109,7 +109,7 @@ str1.match(+Infinity);  // 返回["Infinity"]
 str1.match(-Infinity);  // 返回["-Infinity"]
 str2.match(65);         // 返回["65"]
 str2.match(+65);        // 有正号的number。返回["65"]
-str3.match(null);       // 返回["null"]
+str3.match(null); // 返回["null"]

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html index 98d0f201ed..c358da2585 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html @@ -35,17 +35,17 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/padStart

示例

-
'abc'.padStart(10);         // "       abc"
+
'abc'.padStart(10);         // "       abc"
 'abc'.padStart(10, "foo");  // "foofoofabc"
 'abc'.padStart(6,"123465"); // "123abc"
 'abc'.padStart(8, "0");     // "00000abc"
-'abc'.padStart(1);          // "abc"
+'abc'.padStart(1); // "abc"

Polyfill

如果原生环境不支持该方法,在其他代码之前先运行下面的代码,将创建 String.prototype.padStart() 方法。

-
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
+
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
 if (!String.prototype.padStart) {
     String.prototype.padStart = function padStart(targetLength,padString) {
@@ -62,7 +62,7 @@ if (!String.prototype.padStart) {
             return padString.slice(0,targetLength) + String(this);
         }
     };
-}
+}
 

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html index c0ee77fe21..e412b0da5c 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html @@ -44,7 +44,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/repeat

此方法已添加到 ECMAScript 2015 规范中,并且可能尚未在所有 JavaScript 实现中可用。然而,你可以使用以下代码段对 String.prototype.repeat() 进行填充:

-
if (!String.prototype.repeat) {
+
if (!String.prototype.repeat) {
   String.prototype.repeat = function(count) {
     'use strict';
     if (this == null) {
@@ -83,7 +83,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/repeat
     }
     return rpt;
   }
-}
+}

示例

diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/tolowercase/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/tolowercase/index.html index 7db9f1e146..e8fd231457 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/tolowercase/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/string/tolowercase/index.html @@ -15,7 +15,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/toLowerCase

语法

-
str.toLowerCase()
+
str.toLowerCase()
 

返回值

diff --git a/files/zh-cn/web/javascript/reference/global_objects/symbol/index.html b/files/zh-cn/web/javascript/reference/global_objects/symbol/index.html index fce5989dd7..38506cf5f0 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/symbol/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/symbol/index.html @@ -150,9 +150,9 @@ typeof symObj; // "object"

 {{jsxref("Operators/typeof", "typeof")}}运算符能帮助你识别 symbol 类型

-
typeof Symbol() === 'symbol'
+
typeof Symbol() === 'symbol'
 typeof Symbol('foo') === 'symbol'
-typeof Symbol.iterator === 'symbol'
+typeof Symbol.iterator === 'symbol'
 

Symbol 类型转换

@@ -170,7 +170,7 @@ typeof Symbol.iterator === 'symbol'

Symbols 在 for...in 迭代中不可枚举。另外,{{jsxref("Object.getOwnPropertyNames()")}} 不会返回 symbol 对象的属性,但是你能使用 {{jsxref("Object.getOwnPropertySymbols()")}} 得到它们。

-
var obj = {};
+
var obj = {};
 
 obj[Symbol("a")] = "a";
 obj[Symbol.for("b")] = "b";
@@ -179,14 +179,14 @@ obj.d = "d";
 
 for (var i in obj) {
    console.log(i); // logs "c" and "d"
-}
+}

Symbols 与 JSON.stringify()

当使用 JSON.stringify() 时,以 symbol 值作为键的属性会被完全忽略:

-
JSON.stringify({[Symbol("foo")]: "foo"});
-// '{}'
+
JSON.stringify({[Symbol("foo")]: "foo"});
+// '{}'

更多细节,请看 {{jsxref("JSON.stringify()")}}。

@@ -194,10 +194,10 @@ for (var i in obj) {

当一个 Symbol 包装器对象作为一个属性的键时,这个对象将被强制转换为它包装过的 symbol 值:

-
var sym = Symbol("foo");
+
var sym = Symbol("foo");
 var obj = {[sym]: 1};
 obj[sym];            // 1
-obj[Object(sym)];    // still 1
+obj[Object(sym)]; // still 1

规范

diff --git a/files/zh-cn/web/javascript/reference/global_objects/undefined/index.html b/files/zh-cn/web/javascript/reference/global_objects/undefined/index.html index 20ff8d53a7..2748581673 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/undefined/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/undefined/index.html @@ -101,9 +101,9 @@ if(y === undefined) { // ReferenceError: y is not defined

但是,技术方面看来这样的使用方法应该被避免。JavaScript是一个静态作用域语言,所以,一个变量是否被声明可以通过看它是否在一个封闭的上下文中被声明。唯一的例外是全局作用域,但是全局作用域是被绑定在全局对象上的,所以要检查一个变量是否在全局上下文中存在可以通过检查全局对象上是否存在这个属性(比如使用{{jsxref("Operators/in", "in")}}操作符)。

-
if ('x' in window) {
+
if ('x' in window) {
   // 只有x被全局性的定义 才会这行这些语句
-}
+}

Void操作符和undefined

diff --git a/files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html b/files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html index 87978a0016..5ce3af745f 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/webassembly/index.html @@ -63,10 +63,10 @@ translation_of: Web/JavaScript/Reference/Global_Objects/WebAssembly

下面的示例(请参见GitHub上的Instantiate-streaming.html演示,并查看在线演示)直接从流式底层源传输.wasm模块,然后对其进行编译和实例化,并通过ResultObject实现promise。 由于instantiateStreaming()函数接受对 {{domxref("Response")}} 对象的promise,因此您可以直接向其传递{{domxref("WindowOrWorkerGlobalScope.fetch()")}}调用,然后它将把返回的response传递给随后的函数。

-
var importObject = { imports: { imported_func: arg => console.log(arg) } };
+
var importObject = { imports: { imported_func: arg => console.log(arg) } };
 
 WebAssembly.instantiateStreaming(fetch('simple.wasm'), importObject)
-.then(obj => obj.instance.exports.exported_func())
+.then(obj => obj.instance.exports.exported_func())

返回的ResultObject实例的成员可以被随后访问到,可以调用实例中被导出的方法。

-- cgit v1.2.3-54-g00ecf