Javascript 通过参数设置未定义的参数

Javascript 通过参数设置未定义的参数,javascript,parameters,arguments,Javascript,Parameters,Arguments,我正在尝试编写一个函数,根据先前指定的可选参数更正函数的参数。但我遇到了一个问题。似乎我不能通过参数数组设置变量,除非它们以前以任何方式定义过。下面的代码显示了我面临的问题的一个示例 function foo(a, b, c) { arguments[0] = "lorem"; arguments[1] = "ipsum"; arguments[2] = "dolor"; console.log([a, b, c]); } foo(null);

我正在尝试编写一个函数,根据先前指定的可选参数更正
函数
参数
。但我遇到了一个问题。似乎我不能通过
参数
数组设置变量,除非它们以前以任何方式定义过。下面的代码显示了我面临的问题的一个示例

function foo(a, b, c) {
    arguments[0] = "lorem";
    arguments[1] = "ipsum";
    arguments[2] = "dolor";

    console.log([a, b, c]);
}

foo(null);              // ["lorem", undefined, undefined]
foo(null, null);        // ["lorem", "ipsum",   undefined]
foo(null, null, null);  // ["lorem", "ipsum",   "dolor"]
当记录
参数时
结果总是
[“lorem”、“ipsum”、“dolor”]

有没有办法解决这个问题

我无法直接设置
a
b
c
,因为在
foo
中调用的函数无法访问这些名称。
我的目标是这样的:

function foo(a, b, c) {
    var rules = [];
    // Rule for optional parameter 1 (b)
    // If it equals true the value of b is shifted to the next parameter (c)
    rules[1] = function(val) { return val !== "ipsum"; };
    optionalize(rules);

    console.log([a, b, c]);
}

foo("lorem", "dolor"); // ["lorem", undefined, "dolor"];

参数
数组实际上不是一个数组,而是一个数组。你不能改变它的长度

你尝试做的事情通常是用

a = a || "lorem";
或者,如果您不想替换任何“虚假”的论点,请使用

if (typeof a === "undefined") a = "lorem";

我不确定您想做什么,但参数[0]=a之类的东西?答:“lorem”等?

您可以将参数转换为数组

function foo () {
     var arguments = Array.prototype.slice.call(arguments);
     arguments.push(4);
     console.log(arguments);
}
foo(1,2,3);

这有点奇怪,但这是你的想法吗

function optionalize(fn, options) {
    var i, key, rule;
    for(i = 0; i < options.length; i += 1) {
        key = fn.placement[i];
        rule = fn.ruleset[key];

        // If the rule exists and returns true, shift value to the right.
        if(rule && rule(options[i])) {
            options[i+1] = options[i];
            options[i] = undefined;
        }

        // Assign the numeric index to an alphabet key.
        // Depending on your use case, you may want to substitute a plain Object here and return that, instead of adding non-numeric properties to an Array.
        options[key] = options[i];
    }
}

// Test function
function foo(a, opts) {
    opts = opts || [];
    optionalize(foo, opts);

    console.log([a, opts.b, opts.c]);
}

// Optional argument names, in the order that they would be received.
foo.placement = ['b', 'c'];

// Optionalize rules
foo.ruleset = {
    b: function (val) { return val !== "ipsum"; }
};


// Demonstration
foo('lorem');
foo('lorem', []);
foo('lorem', ['dolor']);
foo('lorem', ['ipsum', 'dolor']);
功能选项化(fn,选项){
var i,键,规则;
对于(i=0;i
dystroy的回答已经指出,
arguments
变量不是一个真正的数组,更改它可能不是一个好主意。我已经提供了一个解决方案,它不依赖于
参数
,并且使用简单的JavaScript尽可能地满足标准


函数
foo
由一个必需的参数
a
指定,后跟一个名为
opts
的可选参数数组。通过
放置
规则集
属性将optionalize规范设置到
foo
。optionalize函数获取此信息,并将数组的索引转换为可用的名称键,根据需要应用规则。

我需要通过arguments数组直接更改变量。您能解释一下为什么必须这样做吗?因为这对我来说似乎不是个好主意,即使它有效。我知道这种方法。但是,如果有多个可选参数,它会变得相当复杂。我编辑了我的答案,这应该会让我的担忧更清楚一点。是的,这是一个很好的解决方案。但是,我们不得不使用额外的数组,这是一个令人沮丧的问题。我相信还有其他方法可以做到这一点。我现在就把这个标记为答案,谢谢。:)