Array.prototype.slice.call(arguments)

本文通过实例解释了如何使用Array.prototype.slice.call将类数组对象转换为真正的数组。这在处理函数参数等类数组对象时非常有用。

    理解Array.prototype.slice.call(arguments)是如何工作的?

    我直接上例子,我就不翻译了,真是怕翻译错了。

   

var a={length:2,0:'first',1:'second'};//类数组,有length属性,长度为2,第0个是first,第1个是second
console.log(Array.prototype.slice.call(a,0));// ["first", "second"],调用数组的slice(0);
var a={length:2,0:'first',1:'second'};
console.log(Array.prototype.slice.call(a,1));//["second"],调用数组的slice(1);
var a={0:'first',1:'second'};//去掉length属性,返回一个空数组
console.log(Array.prototype.slice.call(a,0));//[]

function test(){
  console.log(Array.prototype.slice.call(arguments,0));//["a", "b", "c"],slice(0)
  console.log(Array.prototype.slice.call(arguments,1));//["b", "c"],slice(1)
}
test("a","b","c");

   arguments本身不是一个数组,可以说是有length属性的一个对象(类数组对象),所以需要将其变通,改造成一个数组。

   JS数组用法,参考此链接

 

 

   stackoverflow:原帖

 

    What happens under the hood is that when .slice() is called normally, this is an Array, and then it just iterates the Array, and does its work.

How is this in the .slice() function an Array? Because when you do:

object.method();

...the object automatically becomes the value of this in the method(). So with:

[1,2,3].slice()

...the [1,2,3] Array is set as the value of this in .slice().


But what if you could substitute something else as the this value? As long as whatever you substitute has a numeric .length property, and a bunch of properties that are numeric indices, it should work. This type of object is often called an array-like object.

The .call() and .apply() methods let you manually set the value of this in a function. So if we set the value of this in .slice() to an array-like object.slice() will just assume it's working with an Array, and will do its thing.

Take this plain object as an example.

var my_object ={'0':'zero','1':'one','2':'two','3':'three','4':'four',
    length:5};

This is obviously not an Array, but if you can set it as the this value of .slice(), then it will just work, because it looks enough like an Array for .slice() to work properly.

var sliced =Array.prototype.slice.call( my_object,3);

Example: http://jsfiddle.net/wSvkv/

As you can see in the console, the result is what we expect:

['three','four'];

So this is what happens when you set an arguments object is the this value of .slice(). Because arguments has a .length property and a bunch of numeric indices, .slice() just goes about its work as though it was working with an Array.

【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
### 使用 Array.prototype.slice.callarguments 转换为数组的方法和场景 在 JavaScript 中,`arguments` 是一个类数组对象,它包含了函数调用时传递的所有参数。由于 `arguments` 不是真正的数组,因此无法直接使用数组方法(如 `forEach`、`map` 等)。为了将 `arguments` 转换为真正的数组,可以使用 `Array.prototype.slice.call(arguments)` 方法[^1]。 #### 方法解析 `Array.prototype.slice.call(arguments)` 的作用是借用数组的 `slice` 方法来操作 `arguments` 对象,并将其转换为真正的数组。具体来说: - `Array.prototype.slice` 是数组原型上的方法,用于从已有的数组中返回选定的元素。 - `call` 是函数的一个方法,用于改变函数内部的 `this` 指向。在这里,`call` 将 `Array.prototype.slice` 的 `this` 指向了 `arguments` 对象。 - 通过这种方式,`arguments` 可以被当作数组来处理,从而实现类数组到真数组的转换[^4]。 #### 示例代码 以下是一个简单的示例,展示如何使用 `Array.prototype.slice.call` 将 `arguments` 转换为数组: ```javascript function convertArgumentsToArray() { // 使用 Array.prototype.slice.callarguments 转换为数组 const args = Array.prototype.slice.call(arguments); return args; } console.log(convertArgumentsToArray(1, 2, 3, 4)); // 输出: [1, 2, 3, 4] ``` #### 场景应用 1. **遍历所有参数**:当需要对函数的所有参数进行统一处理时,可以先将 `arguments` 转换为数组,然后使用数组方法(如 `forEach` 或 `map`)进行操作[^2]。 ```javascript function sumAll() { const args = Array.prototype.slice.call(arguments); return args.reduce((acc, num) => acc + num, 0); } console.log(sumAll(1, 2, 3, 4)); // 输出: 10 ``` 2. **实现 rest 参数的功能(兼容旧版本浏览器)**:在 ES6 引入 rest 参数之前,可以通过 `Array.prototype.slice.call(arguments)` 实现类似功能[^3]。 ```javascript function concatStrings() { const args = Array.prototype.slice.call(arguments); return args.join(' '); } console.log(concatStrings('Hello', 'world', '!')); // 输出: "Hello world !" ``` 3. **结合高阶函数**:在需要对 `arguments` 进行复杂处理时,可以将其转换为数组后与高阶函数配合使用。 ```javascript function filterEvenNumbers() { const args = Array.prototype.slice.call(arguments); return args.filter(num => num % 2 === 0); } console.log(filterEvenNumbers(1, 2, 3, 4, 5)); // 输出: [2, 4] ``` #### 注意事项 - 如果只需要部分参数,可以在 `slice` 方法中传入起始索引。例如,`Array.prototype.slice.call(arguments, 1)` 表示从第二个参数开始转换为数组[^4]。 - 在现代 JavaScript 中,推荐使用 rest 参数(`...args`),因为它语法更简洁且性能更好。但当需要兼容旧版本浏览器时,仍可使用 `Array.prototype.slice.call(arguments)`[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值