PHP中有func_num_args和func_get_args, JavaScript中也有类似的东西吗?
当前回答
在ES6中,使用Array.from:
function foo()
{
foo.bar = Array.from(arguments);
foo.baz = foo.bar.join();
}
foo(1,2,3,4,5,6,7);
foo.bar // Array [1, 2, 3, 4, 5, 6, 7]
foo.baz // "1,2,3,4,5,6,7"
对于非es6代码,使用JSON。stringify和JSON.parse:
function foo()
{
foo.bar = JSON.stringify(arguments);
foo.baz = JSON.parse(foo.bar);
}
/* Atomic Data */
foo(1,2,3,4,5,6,7);
foo.bar // "{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7}"
foo.baz // [object Object]
/* Structured Data */
foo({1:2},[3,4],/5,6/,Date())
foo.bar //"{"0":{"1":2},"1":[3,4],"2":{},"3":"Tue Dec 17 2013 16:25:44 GMT-0800 (Pacific Standard Time)"}"
foo.baz // [object Object]
如果需要保存而不是字符串化,则使用内部结构化克隆算法。
如果传递了DOM节点,则在不相关的问题中使用XMLSerializer。
with (new XMLSerializer()) {serializeToString(document.documentElement) }
如果作为bookmarklet运行,则可能需要将每个结构化数据参数包装在JSON的Error构造函数中。Stringify以正常工作。
参考文献
结构克隆CommonJS模块 JS对象克隆 MDN: Array.from ()
其他回答
ES6允许使用“…”符号指定函数参数的构造,例如
function testArgs (...args) {
// Where you can test picking the first element
console.log(args[0]);
}
如果您愿意,还可以将其转换为数组。如果Array泛型可用:
var args = Array.slice(arguments)
否则:
var args = Array.prototype.slice.call(arguments);
来自Mozilla MDN:
您不应该对参数进行切片,因为这会阻止在 JavaScript引擎(例如V8)。
希望这是有用的代码:
function lazyLoadIcons(){
for(let i = 0; i < arguments.length; i++) {
var elements = document.querySelectorAll(arguments[i]);
elements.forEach(function(item){
item.classList.add('loaded');
});
}
}
lazyLoadIcons('.simple-2col', '.ftr-blue-ad', '.btm-numb');
~拉胡尔·达克什
arguments是一个类似数组的对象(不是实际的数组)。例子函数…
function testArguments () // <-- notice no arguments specified
{
console.log(arguments); // outputs the arguments to the console
var htmlOutput = "";
for (var i=0; i < arguments.length; i++) {
htmlOutput += '<li>' + arguments[i] + '</li>';
}
document.write('<ul>' + htmlOutput + '</ul>');
}
试试吧……
testArguments("This", "is", "a", "test"); // outputs ["This","is","a","test"]
testArguments(1,2,3,4,5,6,7,8,9); // outputs [1,2,3,4,5,6,7,8,9]
详情:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments
在ES6中,使用Array.from:
function foo()
{
foo.bar = Array.from(arguments);
foo.baz = foo.bar.join();
}
foo(1,2,3,4,5,6,7);
foo.bar // Array [1, 2, 3, 4, 5, 6, 7]
foo.baz // "1,2,3,4,5,6,7"
对于非es6代码,使用JSON。stringify和JSON.parse:
function foo()
{
foo.bar = JSON.stringify(arguments);
foo.baz = JSON.parse(foo.bar);
}
/* Atomic Data */
foo(1,2,3,4,5,6,7);
foo.bar // "{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7}"
foo.baz // [object Object]
/* Structured Data */
foo({1:2},[3,4],/5,6/,Date())
foo.bar //"{"0":{"1":2},"1":[3,4],"2":{},"3":"Tue Dec 17 2013 16:25:44 GMT-0800 (Pacific Standard Time)"}"
foo.baz // [object Object]
如果需要保存而不是字符串化,则使用内部结构化克隆算法。
如果传递了DOM节点,则在不相关的问题中使用XMLSerializer。
with (new XMLSerializer()) {serializeToString(document.documentElement) }
如果作为bookmarklet运行,则可能需要将每个结构化数据参数包装在JSON的Error构造函数中。Stringify以正常工作。
参考文献
结构克隆CommonJS模块 JS对象克隆 MDN: Array.from ()
推荐文章
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?
- 如何检查动态附加的事件监听器是否存在?
- 大的Ө符号到底代表什么?
- 如何从Python函数中返回两个值?
- 如何写setTimeout与参数Coffeescript
- 将JavaScript字符串中的多个空格替换为单个空格
- 堆与二叉搜索树(BST)
- 给定一个数字数组,返回所有其他数字的乘积的数组(不除法)
- JavaScript: override alert()