[js] apply、call 更多使用实例
传入(继承)参数
function sum() { return Array.prototype.reduce.call(arguments, function(sum, value) { if (value >= 1 && value <= 9) { return sum + value; } else { return sum; } }, 0); } function multiplication() { return Array.prototype.reduce.call(arguments, function(product, value) { return product * value; }, 1); } function filter(min, max) { return Array.prototype.slice.call(arguments, 2).filter(function(value) { return value >= min && value <= max; }); } function filterNoNumbers() { return Array.prototype.filter.call(arguments, function(value) { return typeof value === 'number' && value === value && value !== Number.POSITIVE_INFINITY && value !== Number.NEGATIVE_INFINITY; }); } function round(decimals) { if (arguments.length === 2) { return arguments[1].toFixed(decimals); } else { return Array.prototype.splice.call(arguments, 1).map(function(value) { return value.toFixed(decimals); }); } } console.log(sum.apply(null, filter(1, 9, -3, 1, 0, 4, 8, 9, 12))); //22 console.log(multiplication.apply(null, filter(1, 9, -3, 1, 0, 4, 8, 9, 12))); //288 console.log(sum.apply(null, filter.apply(null, [1, 9].concat(filterNoNumbers(-3, NaN, 1, 0, "2", 4, 8, 9, 12))))); // 22 console.log(round.apply(null, [2].concat(sum.apply(null, filter.apply(null, [1, 9].concat(filterNoNumbers(-3, 1.016, 0, 4, NaN, 8.041, '27', 9, 12))))))); // "22.06"
#
function log() { console.log.apply(console, arguments); }; log('xx','hah');
#
function addCall(a) { var f = function(v) { return v + this.base; } var b = { base: 2 }; return f.call(b, a); //一起做参数传入了f,但是结构是b到a? //return f.call(a, b);//一起做参数传入了f,但是结构是a到b? } console.info(addCall(1)); //3
Array.prototype.slice.call()
[].slice.call()
array.slice(begin[, end])
#常用将多个参数转换为数组然后进行slice,不传begin则取全部
[].slice.call(arguments)
Array.prototype.splice.call()
array.splice(start, deleteCount[, item1[, item2[, ...]]])
只有删除没有了添加功能
第一个参数代表去除0-start的参数,取完
加入第二个代表去除0-start的参数,取deleteCount个
#将多个参数转换为数组然后进行splice
function round(decimals) { if (arguments.length === 2) { return arguments[1].toFixed(decimals); } else { return Array.prototype.splice.call(arguments, 1).map(function(value) { return value.toFixed(decimals); }); } } console.log(round(2, 1, 2.1, 2.346)); // ["1.00", "2.10", "2.35"] console.log(round(2, 1.3)); // "1.30"
Math.max.apply() Math.min.apply()
#数组中的最大值最小值
var numbers = [5, 458, 120, -215, 228, 400, 122205, -85411]; // var maxInNumbers = Math.max.apply(Math, numbers); // var minInNumbers = Math.min.apply(Math, numbers); // var maxInNumbers = Math.max.apply(this, numbers); // var minInNumbers = Math.min.apply(this, numbers); var maxInNumbers = Math.max.apply(null, numbers); var minInNumbers = Math.min.apply(null, numbers); console.log(maxInNumbers); console.log(minInNumbers);
Object.prototype.toString.call()
#类型判别
function isArray(value) { return Object.prototype.toString.call(value) == '[object Array]'; } function isFunction(value) { return Object.prototype.toString.call(value) == '[object Function]'; } function isRegExp(value) { return Object.prototype.toString.call(value) == '[object RegExp]'; } console.log(isRegExp(/abc/));
Array.prototype.push.apply()
#数组之间追加
var array1 = [12, "foo", {name:"Joe"}, -2458]; var array2 = ["Doe", 555, 100]; Array.prototype.push.apply(array1, array2); //array1 值为 [12 , "foo" , {name:"Joe"} , -2458 , "Doe" , 555 , 100]
Array.prototype.reduce.call()
#将参数转换为数组然后进行reduce
function sum() { return Array.prototype.reduce.call(arguments, function(sum, value) { console.log(sum, value); return sum + value; }, 0); } console.log(sum(1, 4, 8, 9)); //22
Array.prototype.filter.call()
#将多个参数转换成数组然后进行filter
//判别是否是数字 function filterNoNumbers() { return Array.prototype.filter.call(arguments, function(value) { return typeof value === 'number' && value === value && value !== Number.POSITIVE_INFINITY && value !== Number.NEGATIVE_INFINITY; }); } filterNoNumbers(-3, NaN, 1, 0, "2", 4, 8, 9, 12); // [-3, 1, 0, 4, 8, 9, 12]
浙公网安备 33010602011771号