다음과 같은 기능이 있습니다.
function f(requiredParamA, requiredParamB, optionalObjectParamA, optionalObjectParamB) {
optionalObjectParamA = optionalObjectParamA || {};
optionalObjectParamB = optionalObjectParamB || {};
// rest of the function
}
에 f
대한 값 으로 전화를 걸고 optionalObjectParamB
싶지만 optionalObjectParamA
. 나는 이것을 할 수있다 :
f("john", 100, null, {vegatarian: true});
그러나 그것은 추악한 API를 만듭니다. 다른 옵션이 있습니까? 함수는 어떻게 디자인해야하며 어떻게 호출해야합니까?
매개 변수가 아닌 개체를 전달합니다. 그러면 매개 변수 목록을 부 풀리지 않고 무제한 선택적 매개 변수를 가질 수 있습니다.
function f(requiredParamA, requiredParamB, context) {
var optional = context || {};
// handle optional.optionalObjectParamA..
// handle optional.optionalObjectParamB..
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다