首页 技术 正文
技术 2022年11月11日
0 收藏 541 点赞 2,633 浏览 2175 个字

1. arguments

  • arguments不是真正的数组,它是一个实参对象,每个实参对象都包含以数字为索引的一组元素以及length属性。

      (function () {
    console.log(arguments instanceof Array); // false
    console.log(typeof (arguments)); // object
    })();
  • 只有函数被调用时,arguments对象才会创建,未调用时其值为null

      console.log(new Function().arguments);  // null
  • JavaScript函数调用不对参数做任何检查。

  • 当调用函数的时候传入的实参比形参个数少时,剩下的形参都被设置为undefined。

给被省略的参数赋值:

function getPropertyNames(o, /* optional */ a) {
a = a || []; // 等于 if(a == undefined) a = [];
for (var property in o) {
a.push(property);
}
return a;
}
console.log(getPropertyNames({ '1': 'a', '2': 'b' })); // ["1", "2"]
console.log(getPropertyNames({ '1': 'a', '2': 'b' }, ['3'])); // ["3", "1", "2"]
  • 当调用函数传入的参数比形参个数多时,通过标识符arguments可以获得未命名的引用。

验证实参的个数

function f(x, y, z) {
// 首先,验证传入实参的个数是否正确
if (arguments.length != 3) {
throw new Error("function f called with " + arguments.length + "arguments, but it expects 3 arguments.");
}
// 再执行函数的其他逻辑
}
f(1, 2); // Uncaught Error: function f called with 2arguments, but it expects 3 arguments.

查看实参和形参的个数是否一致

// 查看实参和形参的个数是否一致
function add(a, b) {
var realLen = arguments.length;
console.log("realLen:", arguments.length); // realLen: 5
var len = add.length;
console.log("len:", add.length); // len: 2
if (realLen == len) {
console.log('实参和形参个数一致');
} else {
console.log('实参和形参个数不一致');
}
};
add(1, 2, 3, 6, 8);

模拟函数重载

function doAdd() {
if (arguments.length == 1) {
console.log(arguments[0] + 5);
} else if (arguments.length == 2) {
console.log(arguments[0] + arguments[1]);
}
}

接受任意数量的实参,并返回其中的最大值

function max(/* ... */) {
var max = Number.NEGATIVE_INFINITY;
// 遍历实参,查找并记住最大值
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
// 返回最大值
return max;
}
console.log(max(1, 10, 110, 2, 3, 5, 10000, 100)); // 10000

自动更新

function f(x) {
console.log(x); // 1,输出实参的初始值
arguments[0] = null; // 修改实参数组的元素同样会修改x的值
console.log(x); // null
x = 'a'; // 修改x的值同样会修改实参数组的元素
console.log(arguments[0]); // a
}
f(1);
  • 实参对象定义了callee和caller属性,callee属性指代正在执行的函数,caller指代调用当前正在执行的函数的函数
  • arguments 的完整语法如下:

    [function.]arguments[n]

    function :当前正在执行的 Function 对象的名字。

    n :要传递给 Function 对象的从0开始的参数值索引。

查看callee和caller属性

function a() {
console.log('a...');
b();
}function b() {
console.log('b...');
console.log(b.arguments.callee);
console.log(b.caller);
}
a();

通过callee实现递归调用自身

var factorial = function (x) {
if (x <= 1) { return 1; }
return x * arguments.callee(x - 1);
}
console.log(factorial(5)); // 120

参考资料

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297