We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在 JavaScript 中并没有重载函数的功能,但每个函数中的 Arguments 对象可以模拟重载的实现。
arguments 不是一个数组对象,没有数组对象所有的属性和方法,但通过 arguments[0]、arguments[1] 等方式获取实参。
arguments
arguments[0]
arguments[1]
function demo() { let str = '' for (let i = 0; i < arguments.length; i++) { str += arguments[i] + ', ' } } console.log(demo('小明', '小红')) // 输出:小明, 小红,
利用 Arguments 对象实现函数重载的方式可以有几种,除了根据参数的个数,还可以根据传入参数的类型、或者利用参数中特殊的参数值来执行不同的操作。
// 通过参数个数实现重载 function overloadDemo() { switch (arguments.length) { case 0: console.log(0) break case 1: console.log(1) break default: console.log(arguments.length) break } } overloadDemo('name') // 输出:1
Arguments 对象的 callee 属性指向的是正在被执行的 Function 对象。常常利用该属性实现递归。
callee
function sum(n) { if (n == 1) { return 1 } else { return n + arguments.callee(n - 1) } } console.log(sum(5)) // 输出:15
但是 arguments.callee 和 arguments.caller 已经在 ES5 严格模式中禁用,将来也会彻底移除。
arguments.callee
arguments.caller
The text was updated successfully, but these errors were encountered:
No branches or pull requests
在 JavaScript 中并没有重载函数的功能,但每个函数中的 Arguments 对象可以模拟重载的实现。
1. 通过下标访问实参:
arguments
不是一个数组对象,没有数组对象所有的属性和方法,但通过arguments[0]
、arguments[1]
等方式获取实参。2. 实现重载
利用 Arguments 对象实现函数重载的方式可以有几种,除了根据参数的个数,还可以根据传入参数的类型、或者利用参数中特殊的参数值来执行不同的操作。
3. callee 属性
Arguments 对象的
callee
属性指向的是正在被执行的 Function 对象。常常利用该属性实现递归。The text was updated successfully, but these errors were encountered: