2
例如,如果我已經定義了這樣的功能:如何檢查參數是否在JavaScript中的函數定義中定義?
function set_name(name) {
return name;
}
但我這樣調用它(因個人原因這樣):
set_name.apply(this, ['Duvdevan', 'Omerowitz']);
什麼將是檢查的最佳實踐和阻止set_name
函數的進一步執行,如果它接受了多於1
參數/參數?
而且,我必須指出,我試圖找出一種方法來檢查這個之前,我打電話.apply
功能:
function set_name(name) {
// if(arguments.length > 1) console.log('This is not what I need though.');
return name;
}
var args = ['Duvdevan', 'Omerowitz'];
// Now, at this point I'd like to check if the set_name function can accept two arguments.
// If not, I'd console.log or return new Error here.
set_name.apply(this, args);
這是可行的?
所以,如果它有多個參數,你想讓它不跑?只有這樣,如果你喜歡你編碼它... – epascarello