函數(shù)的參數(shù)

function.length

此函數(shù)校驗(yàn)參數(shù)個(gè)數(shù)

function __matchArgs__(fn) {
  return function(...args){
    if (args.length !== fn.length){
      throw RangeError('Arguments not match!');
    }
    return fn.apply(this,args);
  }
}

var add= __matchArgs__((a,b,c) => a+b+c );

console.log(add(1,2,3));

console.log(add(1,2));

可變參數(shù)與arguments

function add() {
  let args = Array.from(arguments);
  return args.reduce((a,b) => a+b);
}
console.log(add(1,23,1));

//JavaScript 函數(shù)設(shè)計(jì)中經(jīng)常會(huì)讓參數(shù)允許有不同的類型
function setStyle(el,property,value) {
  if (typeof el ==='string') {
    el = document.querySelector(el);
  }
  if (typeof property === 'object') {
    for (var key in property) {
      setStyle(el,key,property[key]);
    }
  }else{
    el.style[property] = value;
  }
}

console.log(setStyle.length);

setStyle('div',color,'red');

setStyle('div',{
  'fontSize':'32px',
  'backgroundColor':'white'
});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容