자바 스크립트에서 선택적 매개 변수 처리
1, 2 또는 3 개의 매개 변수를 사용할 수있는 정적 자바 스크립트 함수가 있습니다.
function getData(id, parameters, callback) //parameters (associative array) and callback (function) are optional
주어진 매개 변수가 정의되지 않았는지 항상 테스트 할 수 있다는 것을 알고 있지만 전달 된 것이 매개 변수인지 콜백인지 어떻게 알 수 있습니까?
가장 좋은 방법은 무엇입니까?
전달할 수있는 것의 예 :
1:
getData('offers');
2 :
var array = new Array();
array['type']='lalal';
getData('offers',array);
삼:
var foo = function (){...}
getData('offers',foo);
4 :
getData('offers',array,foo);
함수에 몇 개의 인수 가 전달 되었는지 알 수 있으며 두 번째 인수가 함수인지 여부를 확인할 수 있습니다.
function getData (id, parameters, callback) {
if (arguments.length == 2) { // if only two arguments were supplied
if (Object.prototype.toString.call(parameters) == "[object Function]") {
callback = parameters;
}
}
//...
}
이런 식으로 arguments 객체를 사용할 수도 있습니다.
function getData (/*id, parameters, callback*/) {
var id = arguments[0], parameters, callback;
if (arguments.length == 2) { // only two arguments supplied
if (Object.prototype.toString.call(arguments[1]) == "[object Function]") {
callback = arguments[1]; // if is a function, set as 'callback'
} else {
parameters = arguments[1]; // if not a function, set as 'parameters'
}
} else if (arguments.length == 3) { // three arguments supplied
parameters = arguments[1];
callback = arguments[2];
}
//...
}
관심이 있으시면 John Resig 의이 기사 에서 JavaScript에서 메소드 오버로드를 시뮬레이트하는 기술에 대해 살펴보십시오 .
어-그것은 당신이 올바른 순서가 아닌 인수로 함수를 호출한다는 것을 의미합니다 ... 권장하지 않을 것입니다.
대신 객체를 함수에 공급하는 것이 좋습니다.
function getData( props ) {
props = props || {};
props.params = props.params || {};
props.id = props.id || 1;
props.callback = props.callback || function(){};
alert( props.callback )
};
getData( {
id: 3,
callback: function(){ alert('hi'); }
} );
혜택:
- 인수 순서를 설명 할 필요가 없습니다
- 타입 검사를하지 않아도됩니다
- 유형 검사가 필요하지 않으므로 기본값을 정의하기가 더 쉽습니다.
- 덜 두통. 네 번째 인수를 추가했다면 매번 유형 검사를 업데이트해야한다고 가정하십시오.
단점 :
- 코드 리팩토링 시간
선택의 여지가 없다면 함수를 사용하여 객체가 실제로 함수인지 감지 할 수 있습니다 (마지막 예 참조).
참고 : 이것은 기능을 감지하는 올바른 방법입니다.
function isFunction(obj) {
return Object.prototype.toString.call(obj) === "[object Function]";
}
isFunction( function(){} )
따라서 typeof 연산자를 사용하여 두 번째 매개 변수가 배열 또는 함수인지 판별하십시오.
이것은 몇 가지 제안을 줄 수 있습니다 : http://www.planetpdf.com/developer/article.asp?ContentID=testing_for_object_types_in_ja
이것이 일인지 숙제인지 확실하지 않으므로 현재 답변을 드리고 싶지 않지만 typeof가 도움이 될 것입니다.
수신 된 매개 변수의 유형을 확인해야합니다. 어쩌면 arguments
두 번째 매개 변수는 '매개 변수'일 수 있고 때로는 '콜백'일 수 있으며 매개 변수의 이름을 지정하는 것이 잘못 될 수 있으므로 배열 을 사용해야합니다 .
나는 이것이 꽤 오래된 질문이라는 것을 알고 있지만 최근에 이것을 다루었습니다. 이 솔루션에 대해 어떻게 생각하는지 알려주세요.
인수를 강력하게 입력하고 선택 사항으로 사용할 수있는 유틸리티를 만들었습니다. 기본적으로 함수를 프록시로 래핑합니다. 인수를 건너 뛰면 undefined 입니다. 동일한 유형의 옵션 인수가 서로 옆에 여러 개있는 경우 기발 할 수 있습니다 . (사용자 지정 인수 검사를 수행하고 각 매개 변수의 기본값을 지정하기 위해 유형 대신 함수를 전달하는 옵션이 있습니다.)
구현은 다음과 같습니다.
function displayOverlay(/*message, timeout, callback*/) {
return arrangeArgs(arguments, String, Number, Function,
function(message, timeout, callback) {
/* ... your code ... */
});
};
명확성을 위해 다음은 진행중인 작업입니다.
function displayOverlay(/*message, timeout, callback*/) {
//arrangeArgs is the proxy
return arrangeArgs(
//first pass in the original arguments
arguments,
//then pass in the type for each argument
String, Number, Function,
//lastly, pass in your function and the proxy will do the rest!
function(message, timeout, callback) {
//debug output of each argument to verify it's working
console.log("message", message, "timeout", timeout, "callback", callback);
/* ... your code ... */
}
);
};
내 GitHub 리포지토리에서 adjustArgs 프록시 코드를 볼 수 있습니다.
https://github.com/joelvh/Sysmo.js/blob/master/sysmo.js
다음은 리포지토리에서 일부 주석이 복사 된 유틸리티 기능입니다.
/*
****** Overview ******
*
* Strongly type a function's arguments to allow for any arguments to be optional.
*
* Other resources:
* http://ejohn.org/blog/javascript-method-overloading/
*
****** Example implementation ******
*
* //all args are optional... will display overlay with default settings
* var displayOverlay = function() {
* return Sysmo.optionalArgs(arguments,
* String, [Number, false, 0], Function,
* function(message, timeout, callback) {
* var overlay = new Overlay(message);
* overlay.timeout = timeout;
* overlay.display({onDisplayed: callback});
* });
* }
*
****** Example function call ******
*
* //the window.alert() function is the callback, message and timeout are not defined.
* displayOverlay(alert);
*
* //displays the overlay after 500 miliseconds, then alerts... message is not defined.
* displayOverlay(500, alert);
*
****** Setup ******
*
* arguments = the original arguments to the function defined in your javascript API.
* config = describe the argument type
* - Class - specify the type (e.g. String, Number, Function, Array)
* - [Class/function, boolean, default] - pass an array where the first value is a class or a function...
* The "boolean" indicates if the first value should be treated as a function.
* The "default" is an optional default value to use instead of undefined.
*
*/
arrangeArgs: function (/* arguments, config1 [, config2] , callback */) {
//config format: [String, false, ''], [Number, false, 0], [Function, false, function(){}]
//config doesn't need a default value.
//config can also be classes instead of an array if not required and no default value.
var configs = Sysmo.makeArray(arguments),
values = Sysmo.makeArray(configs.shift()),
callback = configs.pop(),
args = [],
done = function() {
//add the proper number of arguments before adding remaining values
if (!args.length) {
args = Array(configs.length);
}
//fire callback with args and remaining values concatenated
return callback.apply(null, args.concat(values));
};
//if there are not values to process, just fire callback
if (!values.length) {
return done();
}
//loop through configs to create more easily readable objects
for (var i = 0; i < configs.length; i++) {
var config = configs[i];
//make sure there's a value
if (values.length) {
//type or validator function
var fn = config[0] || config,
//if config[1] is true, use fn as validator,
//otherwise create a validator from a closure to preserve fn for later use
validate = (config[1]) ? fn : function(value) {
return value.constructor === fn;
};
//see if arg value matches config
if (validate(values[0])) {
args.push(values.shift());
continue;
}
}
//add a default value if there is no value in the original args
//or if the type didn't match
args.push(config[2]);
}
return done();
}
ArgueJS 를 사용하는 것이 좋습니다 .
이런 식으로 함수를 입력하면됩니다.
function getData(){
arguments = __({id: String, parameters: [Object], callback: [Function]})
// and now access your arguments by arguments.id,
// arguments.parameters and arguments.callback
}
I considered by your examples that you want your id
parameter to be a string, right? Now, getData
is requiring a String id
and is accepting the optionals Object parameters
and Function callback
. All the use cases you posted will work as expected.
You can use the arguments object property inside the function.
Are you saying you can have calls like these: getData(id, parameters); getData(id, callback)?
In this case you can't obviously rely on position and you have to rely on analysing the type: getType() and then if necessary getTypeName()
Check if the parameter in question is an array or a function.
I think you want to use typeof() here:
function f(id, parameters, callback) {
console.log(typeof(parameters)+" "+typeof(callback));
}
f("hi", {"a":"boo"}, f); //prints "object function"
f("hi", f, {"a":"boo"}); //prints "function object"
If your problem is only with function overloading (you need to check if 'parameters' parameter is 'parameters' and not 'callback'), i would recommend you don't bother about argument type and
use this approach. The idea is simple - use literal objects to combine your parameters:
function getData(id, opt){
var data = voodooMagic(id, opt.parameters);
if (opt.callback!=undefined)
opt.callback.call(data);
return data;
}
getData(5, {parameters: "1,2,3", callback:
function(){for (i=0;i<=1;i--)alert("FAIL!");}
});
This I guess may be self explanatory example:
function clickOn(elem /*bubble, cancelable*/) {
var bubble = (arguments.length > 1) ? arguments[1] : true;
var cancelable = (arguments.length == 3) ? arguments[2] : true;
var cle = document.createEvent("MouseEvent");
cle.initEvent("click", bubble, cancelable);
elem.dispatchEvent(cle);
}
Can you override the function? Will this not work:
function doSomething(id){}
function doSomething(id,parameters){}
function doSomething(id,parameters,callback){}
참고URL : https://stackoverflow.com/questions/1529077/handling-optional-parameters-in-javascript
'IT' 카테고리의 다른 글
리눅스에서 빈 폴더를 나열하는 방법 (0) | 2020.07.05 |
---|---|
내 GIT 하위 모듈 HEAD가 마스터에서 분리 된 이유는 무엇입니까? (0) | 2020.07.05 |
Gemfile을 찾을 수 없습니다 (0) | 2020.07.05 |
matplotlib에서 컬러 바 범위 설정 (0) | 2020.07.05 |
자바 스크립트에서 지역 / 코드 축소를 구현하는 방법 (0) | 2020.07.05 |