1.概念:arguments是一个类数组对象(概念:它也有索引以及length属性。但是却不能使用数组方法),实际中,通过arguments获取所有的实参,同时也需要对其转换为数组。
属性:
length // 获取长度
callee // 引用当前正在执行的函数
2.类数组转换为数组
slice(begin,end)
splice(start,deleteCount,item1,item2......)
Array.from
Array.prototype.concat.apply([],arguments)
Array.prototype.slice.call(arguments)
3.应用:手动封装bind
Function.prototype.mybind = function(context) {
var self = this;
var bindArgs = [];//1. 保存bind函数调用时,传递的参数
for(var i = 1, len = arguments.length; i< len;i ++) {
bindArgs.push(arguments[i]);
}
console.log("bindargs",bindArgs); // 1
//bind()方法返回值是一个函数
return function() {
//2.保存原函数,传递的参数
var funArgs = Array.prototype.slice.call(arguments);
console.log("funarg",funArgs); // 2
self.apply(context, bindArgs.concat(funArgs))
}
}
function fn(){};
fn.mybind(this,1)(2);