xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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)  => {
  // 
}


setTimeout

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])


Functions arguments

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);

callee & caller

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

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)

demos

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`)
}


ES6 arrow function 不存在 arguments 对象

Flag Counter

©xgqfrms 2012-2020

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


原文地址:https://www.cnblogs.com/xgqfrms/p/13219071.html