- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了以下代码:
var f = function () {
var args = Array.prototype.slice.call(arguments).splice(1);
// some more code
};
基本上,args
中的结果是一个数组,它是没有第一个元素的 arguments
的副本。
但我无法确切理解的是为什么 f
的 arguments
(这是一个将函数的输入参数保存到类似数组的对象中的对象)对象被传递给 slice
方法,以及 slice(1)
如何删除第一个元素(位于索引 0 处)。
谁能帮我解释一下吗?
P.S.代码来自这个partial application function
最佳答案
<注意>
实际代码来自 linked answer是:
var args = Array.prototype.slice.call(arguments, 1);
即“切片”,而不是“拼接”
首先,slice
方法常用于make a copy of the array it's called on :
var a = ['a', 'b', 'c'];
var b = a.slice(); // b is now a copy of a
var c = a.slice(1); // c is now ['b', 'c']
所以简短的答案是代码基本上是在模拟:
arguments.slice(1); // discard 1st argument, gimme the rest
但是你不能直接这样做。 special arguments
object (在所有 JavaScript 函数的执行上下文中可用),尽管类似于数组,因为它支持通过 []
进行索引带数字键的运算符,实际上不是数组;你不能.push
到它上面,.pop
关闭它,或.slice
它等等
代码实现此目的的方式是“欺骗” slice
函数(在 arguments
对象上同样不可用)在 arguments
的上下文中运行 ,通过 Function.prototype.call
:
Array.prototype.slice // get a reference to the slice method
// available on all Arrays, then...
.call( // call it, ...
arguments, // making "this" point to arguments inside slice, and...
1 // pass 1 to slice as the first argument
)
Array.prototype.slice.call(arguments).splice(1)
完成同样的事情,但对 splice(1)
进行了额外的调用,它从 Array.prototype.slice.call(arguments)
返回的数组中删除元素从索引 1
开始并继续到数组的末尾。 splice(1)
在 IE 中不起作用(从技术上讲,它缺少第二个参数,告诉它需要删除 IE 和 ECMAScript 需要多少个项目)。
关于javascript - 关于 JavaScript 的 slice 和 splice 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1777705/
我正在使用 JavaScript splice() 方法从数组中删除一个元素。代码如下, var fruits = ["Banana", "Orange", "Apple", "Mango"]; fu
JavaScript 拼接方法实际上将数组拆分为两个,如下例所示: var a = ['a','b','c']; var b = a.splice(0,2); console.log(a); // o
我正在尝试从包含表单输入字段的数组中删除某些值: allFields = theForm.getElementsByTagName("INPUT"); for(j = 0; j =0; j--){
var arr = document.querySelectorAll("a[href*='somestring']") 返回控制台中看起来像数组的内容。方括号 [] 和 arr.length = 7
我正在尝试从 javascript 中的数组中删除特定项目,但似乎无法使 [array].splice 函数正常工作。 此代码用于检查 SVG 对象是否与另一个对象发生碰撞(对于游戏)。玩家总是希望与
所以这是一个语法问题,我一直在阅读 MDN 文档,但我找不到类似的例子。我从这里得到了这个特定的代码片段:https://bost.ocks.org/mike/shuffle/正如我正在阅读随机数组一
这个问题在这里已经有了答案: What is the difference between call and apply? (23 个回答) 关闭 8 年前。 我正在尝试将可在函数内部访问的 arg
我有一个数组,它在任何时候都可能包含以下值的任意组合: var positions = ['first', 'second', 'third', 'fourth']; 目标是重建一个 Javascri
我运行它是为了获取 HTML 表单控制元素的集合 var elem = document.getElementById('display').elements; 我可以运行它来获取一组元素: var
如何正确使用Array.Splice? var myEmptyData = []; myEmptyData = $.parseJSON('[{ "m_sPartNumber": "B104-9
这个问题已经有答案了: Why does changing an Array in JavaScript affect copies of the array? (13 个回答) 已关闭 7 年前。
我正在为每个单击的导航容器设置一个 cookie。 它设置一个连接的数组并设置cookie值。如果再次单击,则将其从数组中删除。 它有点问题。 只有点击其他元素后才会拼接。然后它的行为就很奇怪。 可能
Javascript(使用 jQuery): var paragraphs = [ ['This is my first paragraph content of the first arra
如果我正在制作一款游戏,并且我希望某些内容仅每隔一段时间更新一次,那么解决此问题的最佳方法是什么? 我天真的想法只是保留一个通过 mod 循环的“帧”跟踪器。数学并以特定数字更新特定内容。 有更好的方
这个问题已经有答案了: How do you clone an array of objects in JavaScript? (33 个回答) using splice(0) to duplicat
我有数组 x、y 和 z。在遍历 x 时,根据条件我需要不断从 z 中删除元素。这是我正在尝试做的事情: var x = ["test0", "test1", "test2"]; var y = ["
我有 3 个包含时间段的数组,它们是字符串...像这样: $scope.shiftMorning = ['09:00', '09:30', '10:00', '10:30', '11:00', '11
我在我的表单上创建了一个功能,我可以通过单击一个按钮为多个图像添加多个文件输入,这按预期工作。现在,当我尝试使用 .splice 删除输入字段时,它会不断删除我的输入字段所在的数组的最后一项,而不是具
我必须向数组添加一些值。 代码示例: temp[0]=new Array("0","0"); temp[1]=new Array("0","0"); temp[2]=new Array("0","0"
我有一个包含字符串的数组,如果字符串中没有任何内容,我想使用 .splice() 将其从数组中删除。 不知何故,它没有得到所有空条目。如果开头有 2 个空条目,它只会得到一个。 这是我的 fiddle
我是一名优秀的程序员,十分优秀!