gpt4 book ai didi

Javascript 参数转移

转载 作者:数据小太阳 更新时间:2023-10-29 04:14:45 28 4
gpt4 key购买 nike

假设我们有以下函数:

var a = function(data, type){
var shift = [].shift;
shift.call(arguments);
shift.call(arguments);
shift.call(arguments);
shift.call(arguments);
console.log(data);
}

a(1,'test', 2, 3);

我理解数据和类型只是对参数中特定值的引用。但是为什么最后data等于3呢?

最佳答案

来自 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Making_eval_and_arguments_simpler :

Strict mode makes arguments less bizarrely magical.

In normal code within a function whose first argument is arg, setting arg also sets arguments[0], and vice versa (unless no arguments were provided or arguments[0] is deleted).

arguments objects for strict mode functions store the original arguments when the function was invoked. arguments[i] does not track the value of the corresponding named argument, nor does a named argument track the value in the corresponding arguments[i].

您实际上遇到了“除非参数[i] 被删除” 情况;)

关于Javascript 参数转移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30459079/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com