1.定义log,然后它可以代理console.log的方法
function log(msg){
console.log(msg);
}
2.传入多个参数,强调传入参数的个数是不定的
function log(){
console.log.apply(console,arguments);
}
这里强调一下console是一个对象。
3. 添加一个”hello”的前辍
function log(){
var args = Array.prototype.slice.call(arguments);
args.unshift('hello');
console.log.apply(console, args);
}
4.数组内部slice的实现方法
Array.prototype.slice = function(start, end) {
var result = new Array();
var start = start || 0;
var end = start || this.length;
for(var i = start;i < end; i++)
result.push(this[i]);
return result;
}
5.js里面实现将伪数组转换成数组的几种方法
1.
function Switcharray() {
var result = [];
for(var i =0; i<arguments.length;i++) {
result[result.length] = arguments[i];
}
return result;
}
2.
var args = Array.prototype.slice.call(arguments);
args就是一个数组了。
3.
ES6中数组的新方法 from()
function test(){
var arg = Array.from(arguments);
arg.push(5);
console.log(arg);//1,2,3,4,5
}
test(1,2,3,4);