为什么这个 ES6 默认参数值没有给出期望的结果?
Why doesn't this ES6 default argument value give the desired result?
我在一本书中找到了这段 ES6 代码:
let value = 5;
function getValue() {
return value++;
}
function add(first, second = getValue()) {
return first + second;
}
console.log(add(1, 1)); // 2
console.log(add(1)); // 6
为什么 运行ning console.log(add(1));
return 6 作为值,这意味着它将参数 second
作为 5,尽管我的代码明确指定 getValue
函数应该 return value++
- 这意味着第一次是 运行,getValue
函数应该是 returning 6 和 add
应该是 returning 7. 我在 Firefox 控制台中 运行ning 这段代码 - 我错过了什么吗?
value++
在增加之前获取值,你应该这样做
++value
或
value+=1;
return value
默认参数 are evaluated at call time,意味着函数 getValue
会在您每次调用 add
时调用 - 而不是在代码最初 运行。由于您使用的是后缀增量,因此 getValue
将在您第一次调用 add
时 return 5,然后是 6、7 等。后缀增量 return 是先前的值,然后递增,例如:
var x = 5
var foo = x++;
这里,foo
赋值5,然后x
递增。因此,在您的示例中,getValue
函数实际上是 returning 5 而不是 6,然后在您首次调用它时递增 value
。如果你想要 6、7、8,使用前缀递增,returns 递增后的值 :
++x;
这将增加 value
,然后 return 该增加的值。您甚至可以使用复合加法:
x += 1;
这会在您再次访问它之前明确地重新分配 x
。进一步阅读这些运算符 here.
因为你使用了后缀表达式,如果你使用前缀表达式那么你会得到7.
"如果使用后缀,操作数后有运算符(例如,x++),则它 returns 递增前的值。
如果在操作数前使用带运算符的前缀(例如,++x),则它 returns 递增后的值。
我认为您的问题与 ecmascript-6
无关,但未能正确理解 ++
运算符。
根据 documentation:
Unary operator. Adds one to its operand. If used as a prefix operator
(++x), returns the value of its operand after adding one; if used as a
postfix operator (x++), returns the value of its operand before adding
one.
将 value++
替换为 ++value
或 value + 1
应该可以解决您的问题。
您好,这称为 'Default value' 参数。这意味着您可以为函数定义中的每个参数设置默认值。
function add(first, second = getValue()) {
return first + second;
}
在这种情况下 'second = getValue()','getValue()' 是“第二个”参数的默认值。
当你
console.log(add(1, 1)); // 2
因为
第一==>1
第二==>1
console.log(add(1)); // 6
因为
第一==>1
second==>getValue() ===> 6
我在一本书中找到了这段 ES6 代码:
let value = 5;
function getValue() {
return value++;
}
function add(first, second = getValue()) {
return first + second;
}
console.log(add(1, 1)); // 2
console.log(add(1)); // 6
为什么 运行ning console.log(add(1));
return 6 作为值,这意味着它将参数 second
作为 5,尽管我的代码明确指定 getValue
函数应该 return value++
- 这意味着第一次是 运行,getValue
函数应该是 returning 6 和 add
应该是 returning 7. 我在 Firefox 控制台中 运行ning 这段代码 - 我错过了什么吗?
value++
在增加之前获取值,你应该这样做
++value
或
value+=1;
return value
默认参数 are evaluated at call time,意味着函数 getValue
会在您每次调用 add
时调用 - 而不是在代码最初 运行。由于您使用的是后缀增量,因此 getValue
将在您第一次调用 add
时 return 5,然后是 6、7 等。后缀增量 return 是先前的值,然后递增,例如:
var x = 5
var foo = x++;
这里,foo
赋值5,然后x
递增。因此,在您的示例中,getValue
函数实际上是 returning 5 而不是 6,然后在您首次调用它时递增 value
。如果你想要 6、7、8,使用前缀递增,returns 递增后的值 :
++x;
这将增加 value
,然后 return 该增加的值。您甚至可以使用复合加法:
x += 1;
这会在您再次访问它之前明确地重新分配 x
。进一步阅读这些运算符 here.
因为你使用了后缀表达式,如果你使用前缀表达式那么你会得到7.
"如果使用后缀,操作数后有运算符(例如,x++),则它 returns 递增前的值。 如果在操作数前使用带运算符的前缀(例如,++x),则它 returns 递增后的值。
我认为您的问题与 ecmascript-6
无关,但未能正确理解 ++
运算符。
根据 documentation:
Unary operator. Adds one to its operand. If used as a prefix operator (++x), returns the value of its operand after adding one; if used as a postfix operator (x++), returns the value of its operand before adding one.
将 value++
替换为 ++value
或 value + 1
应该可以解决您的问题。
您好,这称为 'Default value' 参数。这意味着您可以为函数定义中的每个参数设置默认值。
function add(first, second = getValue()) {
return first + second;
}
在这种情况下 'second = getValue()','getValue()' 是“第二个”参数的默认值。
当你
console.log(add(1, 1)); // 2
因为
第一==>1
第二==>1
console.log(add(1)); // 6
因为 第一==>1 second==>getValue() ===> 6