From 01b0e12ba27b5069248fd09235e9a7143915ee30 Mon Sep 17 00:00:00 2001 From: Irvin Date: Wed, 16 Feb 2022 02:02:49 +0800 Subject: remove `notranslate` class in zh-CN --- .../reference/functions/arguments/index.html | 36 +++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/functions/arguments/index.html') diff --git a/files/zh-cn/web/javascript/reference/functions/arguments/index.html b/files/zh-cn/web/javascript/reference/functions/arguments/index.html index 0cc7216837..8aff3819bd 100644 --- a/files/zh-cn/web/javascript/reference/functions/arguments/index.html +++ b/files/zh-cn/web/javascript/reference/functions/arguments/index.html @@ -35,18 +35,18 @@ translation_of: Web/JavaScript/Reference/Functions/arguments

arguments对象是所有(非箭头)函数中都可用的局部变量。你可以使用arguments对象在函数中引用函数的参数。此对象包含传递给函数的每个参数,第一个参数在索引0处。例如,如果一个函数传递了三个参数,你可以以如下方式引用他们:

-
arguments[0]
+
arguments[0]
 arguments[1]
 arguments[2]
 

参数也可以被设置:

-
arguments[1] = 'new value';
+
arguments[1] = 'new value';

arguments对象不是一个 {{jsxref("Array")}} 。它类似于Array,但除了length属性和索引元素之外没有任何Array属性。例如,它没有 pop 方法。但是它可以被转换为一个真正的Array

-
var args = Array.prototype.slice.call(arguments);
+
var args = Array.prototype.slice.call(arguments);
 var args = [].slice.call(arguments);
 
 // ES2015
@@ -57,7 +57,7 @@ const args = [...arguments];
 

对参数使用slice会阻止某些JavaScript引擎中的优化 (比如 V8 - 更多信息)。如果你关心性能,尝试通过遍历arguments对象来构造一个新的数组。另一种方法是使用被忽视的Array构造函数作为一个函数:

-
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
+
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
 
@@ -67,7 +67,7 @@ const args = [...arguments];

typeof参数返回 'object'。

-
console.log(typeof arguments);    // 'object'
+
console.log(typeof arguments);    // 'object'
 // arguments 对象只能在函数内使用
 function test(a){
     console.log(a,Object.prototype.toString.call(arguments));
@@ -83,13 +83,13 @@ number
 
 

可以使用索引确定单个参数的类型。

-
console.log(typeof arguments[0]); //this will return the typeof individual arguments.
+
console.log(typeof arguments[0]); //this will return the typeof individual arguments.

对参数使用扩展语法

您还可以使用{{jsxref("Array.from()")}}方法或扩展运算符将参数转换为真实数组:

-
var args = Array.from(arguments);
+
var args = Array.from(arguments);
 var args = [...arguments];

属性

@@ -120,7 +120,7 @@ var args = [...arguments];

遍历参数求和

-
function add() {
+
function add() {
     var sum =0,
         len = arguments.length;
     for(var i=0; i<len; i++){
@@ -136,14 +136,14 @@ add(1,2,3,4);                   // 10

这个例子定义了一个函数来连接字符串。这个函数唯一正式声明了的参数是一个字符串,该参数指定一个字符作为衔接点来连接字符串。该函数定义如下:

-
function myConcat(separator) {
+
function myConcat(separator) {
   var args = Array.prototype.slice.call(arguments, 1);
   return args.join(separator);
 }

你可以传递任意数量的参数到该函数,并使用每个参数作为列表中的项创建列表。

-
// returns "red, orange, blue"
+
// returns "red, orange, blue"
 myConcat(", ", "red", "orange", "blue");
 
 // returns "elephant; giraffe; lion; cheetah"
@@ -156,7 +156,7 @@ myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");

这个例子定义了一个函数通过一个字符串来创建HTML列表。这个函数唯一正式声明了的参数是一个字符。当该参数为 "u" 时,创建一个无序列表 (项目列表);当该参数为 "o" 时,则创建一个有序列表 (编号列表)。该函数定义如下:

-
function list(type) {
+
function list(type) {
   var result = "<" + type + "l><li>";
   var args = Array.prototype.slice.call(arguments, 1);
   result += args.join("</li><li>");
@@ -167,7 +167,7 @@ myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");

你可以传递任意数量的参数到该函数,并将每个参数作为一个项添加到指定类型的列表中。例如:

-
var listHTML = list("u", "One", "Two", "Three");
+
var listHTML = list("u", "One", "Two", "Three");
 
 /* listHTML is:
 
@@ -180,7 +180,7 @@ myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");

arguments对象可以与剩余参数默认参数解构赋值参数结合使用。

-
function foo(...args) {
+
function foo(...args) {
   return args;
 }
 foo(1, 2, 3);  // [1,2,3]
@@ -190,7 +190,7 @@ foo(1, 2, 3);  // [1,2,3]
 
 

当非严格模式中的函数没有包含剩余参数默认参数解构赋值,那么arguments对象中的值跟踪参数的值(反之亦然)。看下面的代码:

-
function func(a) {
+
function func(a) {
   arguments[0] = 99;   // 更新了arguments[0] 同样更新了a
   console.log(a);
 }
@@ -199,7 +199,7 @@ func(10); // 99
 
 

并且

-
function func(a) {
+
function func(a) {
   a = 99;              // 更新了a 同样更新了arguments[0]
   console.log(arguments[0]);
 }
@@ -208,7 +208,7 @@ func(10); // 99
 
 

当非严格模式中的函数包含剩余参数默认参数解构赋值,那么arguments对象中的值不会跟踪参数的值(反之亦然)。相反, arguments反映了调用时提供的参数:

-
function func(a = 55) {
+
function func(a = 55) {
   arguments[0] = 99; // updating arguments[0] does not also update a
   console.log(a);
 }
@@ -216,7 +216,7 @@ func(10); // 10

并且

-
function func(a = 55) {
+
function func(a = 55) {
   a = 99; // updating a does not also update arguments[0]
   console.log(arguments[0]);
 }
@@ -224,7 +224,7 @@ func(10); // 10

并且

-
function func(a = 55) {
+
function func(a = 55) {
   console.log(arguments[0]);
 }
 func(); // undefined
-- 
cgit v1.2.3-54-g00ecf