/**
* callee: 宿主函数 test
* Symbol.iterator 可迭代对象标志
*
* 类数组 Array like
* 有 length 从 0 开始的属性下标
* 没有数组的内置方法(build-in methods/object)
*
*/
function test() {
console.log(arguments);
console.log(arguments.toString()); // [object Arguments]
console.log(Array.isArray(arguments)); // false
console.log(arguments.callee);
}
test();
var obj = {
a: 1,
b: 2,
c: 3
}
function * generator(args) {
for (const item of args) {
yield item;
}
}
var it = generator(obj);
it.next();
function * generator(args) {
for (const item of args) {
yield item;
}
}
function test() {
var it = generator(arguments);
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
}
test(1, 2, 3);
var test = () => {
console.log(arguments);
}
test();
改用剩余参数:
var test = (...args) => {
console.log(args);
console.log(Array.isArray(args));
}
test(1, 2, 3);
var test = (...args) => {
// console.log(arguments.callee); // 直接拿 test 能拿到,不需要 .callee
console.log(test);
}
test(1, 2, 3);
function test() {
// var params = Array.prototype.slice.call(arguments); // ↓简便写法
var params = [].slice.call(arguments); // 把 arguments 当 [] 去调用 slice
console.log(params);
}
test(1, 2, 3);
function test() {
var params = arguments.length === 1
? [arguments[0]]
: Array.apply(null, arguments); // Array是构造函数,传递null当this,参数是arguments
console.log(params);
}
test(1, 2, 3);
function test(a, b, c) {
console.log(arguments[3]);
}
test(1, 2, 3, 4);
function add() {
return [...arguments].reduce((pre, cur) => pre + cur, 0);
}
const res = add(1, 2, 3);
console.log(res);
形参赋值的有内部作用域
function test(a) {
arguments[0] = 10;
console.log(a, arguments[0]);
}
test(1);
function test(a) {
a = 10;
console.log(a, arguments[0]);
}
test(1);
function test(a = 100) {
arguments[0] = 10;
console.log(a, arguments[0]);
}
test(1);
function test(a = 100) {
a = 10000;
console.log(a, arguments[0]);
}
test(1);
function test(a, b, c = 10) {
arguments[0] = 100;
arguments[1] = 200;
arguments[2] = 300;
console.log(a, arguments[0]);
console.log(b, arguments[1]);
console.log(c, arguments[2]);
}
test(1, 2, 3);
function test(...args) {
arguments[0] = 100;
arguments[1] = 200;
arguments[2] = 300;
console.log(args[0], arguments[0]);
console.log(args[1], arguments[1]);
console.log(args[2], arguments[2]);
}
test(1, 2, 3);
function test({ a, b, c }) {
arguments[0] = 100;
arguments[1] = 200;
arguments[2] = 300;
console.log(a, arguments[0]);
console.log(b, arguments[1]);
console.log(c, arguments[2]);
}
test({
a: 1,
b: 2,
c: 3
});
function test(a, b, c) {
'use strict';
a = 10;
b = 20;
c = 30;
console.log(a, b, c);
console.log([...arguments]);
}
test(1, 2, 3);
function test(a, b, c) {
'use strict';
arguments[0] = 10;
arguments[1] = 20;
arguments[2] = 30;
console.log(a, b, c);
console.log([...arguments]);
}
test(1, 2, 3);