Javascript 添加额外的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13610987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript add extra argument
提问by onlineracoon
Lets take a look at this code:
让我们来看看这段代码:
var mainFunction = function() {
altFunction.apply(null, arguments);
}
The arguments that are passed to "mainFunction" are dynamic they can be 4 or 10 doesnt matter. However I have to pass them through to altFunction AND I have to add an EXTRA argument to the argument list.
传递给“mainFunction”的参数是动态的,它们可以是 4 或 10 个无关紧要。但是我必须将它们传递给 altFunction 并且我必须在参数列表中添加一个 EXTRA 参数。
I have tried this:
我试过这个:
var mainFunction = function() {
var mainArguments = arguments;
mainArguments[mainArguments.length] = 'extra data'; // not +1 since length returns "human" count.
altFunction.apply(null, mainArguments);
}
But that does not seem to work, how can I do this?
但这似乎不起作用,我该怎么做?
采纳答案by VisioN
argumentsis not a pure array. You need to make a normal array out of it:
arguments不是纯数组。您需要从中制作一个普通数组:
var mainArguments = Array.prototype.slice.call(arguments);
mainArguments.push("extra data");
回答by I Hate Lazy
Use Array.prototype.push
用 Array.prototype.push
[].push.call(arguments, "new value");
There's no need to shallow clone the argumentsobject because it and its .lengthare mutable.
没有必要浅克隆arguments对象,因为它和它的.length都是可变的。
(function() {
console.log(arguments[arguments.length - 1]); // foo
[].push.call(arguments, "bar");
console.log(arguments[arguments.length - 1]); // bar
})("foo");
From ECMAScript 5, 10.6 Arguments Object
来自 ECMAScript 5, 10.6 Arguments 对象
- Call the
[[DefineOwnProperty]]internal method on obj passing"length", the Property Descriptor{[[Value]]: len, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}, and false as arguments.
- 调用
[[DefineOwnProperty]]obj 传递的内部方法"length"、属性描述符{[[Value]]: len, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}和 false 作为参数。
So you can see that .lengthis writeable, so it will update with Array methods.
所以你可以看到它.length是可写的,所以它会用 Array 方法更新。
回答by Pointy
The argumentsobject isn't an array; it's likean array, but it's different. You can turn it into an array however:
该arguments对象不是数组;它就像一个数组,但又有所不同。但是,您可以将其转换为数组:
var mainArguments = [].slice.call(arguments, 0);
Then you can push another value onto the end:
然后你可以将另一个值推到最后:
mainArguments.push("whatever");
回答by Greg
Update 2016: You must convert the arguments to an array before adding the element. In addition to the slice method mentioned in many posts:
2016 年更新:您必须在添加元素之前将参数转换为数组。除了很多帖子提到的slice方法:
var args = Array.prototype.slice.call(arguments);
You can also use the Array.from() method or the spread operator to convert arguments to a real Array:
您还可以使用 Array.from() 方法或扩展运算符将参数转换为真正的数组:
var args = Array.from(arguments);
or
或者
var args = [...arguments];
The above may not be optimized by your javascript engine, it has been suggested by the MDN the following may be optimized:
以上内容可能未由您的 javascript 引擎优化,MDN 已建议以下内容可能会优化:
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
回答by callumacrae
The arguments"array" isn't an array (it's a design bug in JavaScript, according to Crockford), so you can't do that. You can turn it into an array, though:
在arguments“阵列”是不是一个数组(这是一个JavaScript设计缺陷,根据克罗克福德),所以你不能做到这一点。不过,您可以将其转换为数组:
var mainFunction = function() {
var mainArguments = Array.prototype.slice.call(arguments);
mainArguments.push('extra data');
altFunction.apply(null, mainArguments);
}
回答by Esailija
var mainFunction = function() {
var args = [].slice.call( arguments ); //Convert to array
args.push( "extra data");
return altFunction.apply( this, args );
}
回答by gdub
One liner to add additional argument(s) and return the new array:
一个用于添加额外参数并返回新数组的衬垫:
[].slice.call(arguments).concat(['new value']));
回答by public override
//
// var
// altFn = function () {},
// mainFn = prefilled( altFn /* ...params */ );
//
// mainFn( /* ...params */ );
//
//
function prefilled ( fn /* ...params */ ) {
return ( function ( args1 ) {
var orfn = this;
return function () {
return orfn.apply( this, args1.concat( cslc( arguments ) ) );
};
} ).call( fn, cslc( arguments, 1 ) );
}
// helper fn
function cslc( args, i, j ) {
return Array.prototype.slice.call( args, i, j );
}
// example
var
f1 = function () { console.log( cslc( arguments ) ); },
F1 = prefilled( f1, 98, 99, 100 );
F1( 'a', 'b', 'c' );
//
// logs: [98, 99, 100, "a", "b", "c"]
//
//
回答by Zon
In this case it could be more comfortable to use call()instead of apply():
在这种情况下,使用call()而不是apply():
function first(parameter1, parameter2) {
var parameter3 = "123";
secondFunction.call(
this,
parameter1,
parameter2,
parameter3);
},
回答by Alan Dong
var myABC = '12321';
someFunction(result, error, myCallback.bind(this, myABC));
var myABC = '12321';
someFunction(result, error, myCallback.bind(this, myABC));
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
参考:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

