Javascript add extra argument

后端 未结 11 1034
暖寄归人
暖寄归人 2021-02-06 22:02

Lets take a look at this code:

var mainFunction = function() {
  altFunction.apply(null, arguments);
}

The arguments that are passed to \"mainF

11条回答
  •  感情败类
    2021-02-06 22:25

    For those who like me was looking for a way to add an argument that is optional and may be not the only omitted one (myFunc = function(reqiured,optional_1,optional_2,targetOptional) with the call like myFunc(justThisOne)), this can be done as follows:

    // first we make sure arguments is long enough
    // argumentPosition is supposed to be 1,2,3... (4 in the example above)
    while(arguments.length < argumentPosition)
        [].push.call(arguments,undefined);
    // next we assign it
    arguments[argumentPosition-1] = arguments[argumentPosition-1] || defaultValue;
    

提交回复
热议问题