JavaScript 说说arguments

面试中遇到了这样一个问题,在我在编码中使用arguments时,面试官顺便问道:arguments和数组的区别是什么。我记得arguments是伪数组,然后……就没有然后了。

实际上,关于argument,MDN是这么说的:

The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array:
var args = Array.prototype.slice.call(arguments);

翻译起来说,也就是说,他除了数组所具有的length属性外,别的什么都没有,如果我们需要使用,需要调用call或者apply,或者把arguments转换为真数组后操作。

植入部分

如果您觉得文章不错,可以通过赞助支持我。

如果您不希望打赏,也可以通过关闭广告屏蔽插件的形式帮助网站运作。

标签: 知识

添加新评论