how to tell a function arguments length in js
阅读原文时间:2023年07月08日阅读:2

how to tell a function arguments length in js

JavaScript函数不对参数值(参数)执行任何检查

https://www.w3schools.com/js/js_function_parameters.asp

// ES5

function functionName(parameter1, parameter2, parameter3) {
  // code to be executed
}

// ES6

const func = (parameter1, parameter2, parameter3, ...rest)  => {
  //
}

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout

setTimeout()

setInterval()

setImmediate()

requestAnimationFrame()

https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Code_snippets/Timers

var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]);
var timeoutID = scope.setTimeout(function[, delay]);
var timeoutID = scope.setTimeout(code[, delay]);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

const start = Date.now();
console.log('starting timer...', start);
// "starting timer..." 1593588039392

const timer = new Date().getTime();
console.log('timer...', timer);
// "timer..." 1593588039392

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

let new_array = arr.map(function callback( currentValue[, index[, array]]) {
    // return element for new_array
}[, thisArg])

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

convert arguments to a real Array

var args = Array.prototype.slice.call(arguments);
//  array literal
var args = [].slice.call(arguments); 

// ES6 Array.from()
let args = Array.from(arguments);

// ES6 spread syntax
let args = [...arguments];




const log = console.log;

function func1(a, b, c) {
  log(`arguments`, arguments, arguments.length);
  log(arguments[0]);
  log(arguments[1]);
  log(arguments[2]);
}

func1(1, 2, 3);

被调用者(接受) & 调用者(发出)

const log = console.log;

function func(a, b, c) {
  log(`arguments`, arguments, arguments.length);
  log(arguments[0]);
  log(arguments[1]);
  log(arguments[2]);
}

func(1,2,3) ;




const log = console.log;

const obj = {
  name: 'abc',
  getName: () => log(this.name),
};

obj.getName();


const log = console.log;

const obj = {
  name: 'abc',
  getName: function() {
   log(`arguments`, arguments, arguments.length);
   log(this.name);
  },
};

obj.getName();

'caller', 'callee', and 'arguments'

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or

the arguments objects for calls to them at Function.invokeGetter (:1:142)

let cb = v => console.log(v || `default value`)
// cb = v => console.log(arguments.length, v || `default value`)

function func(v) {
  console.log(arguments.length, v || `default value`)
}


xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!