콜백으로 사용되는 함수에 일부 매개 변수를 전달하려고하는데 어떻게해야합니까?
function tryMe (param1, param2) {
alert (param1 + " and " + param2);
}
function callbackTester (callback, param1, param2) {
callback (param1, param2);
}
callbackTester (tryMe, "hello", "goodbye");
콜백으로 사용되는 함수에 일부 매개 변수를 전달하려고하는데 어떻게해야합니까?
function tryMe (param1, param2) {
alert (param1 + " and " + param2);
}
function callbackTester (callback, param1, param2) {
callback (param1, param2);
}
callbackTester (tryMe, "hello", "goodbye");
답변:
좀 더 일반적인 것을 원한다면 arguments 변수를 다음과 같이 사용할 수 있습니다.
function tryMe (param1, param2) {
alert(param1 + " and " + param2);
}
function callbackTester (callback) {
callback (arguments[1], arguments[2]);
}
callbackTester (tryMe, "hello", "goodbye");
그러나 그렇지 않으면 예제가 잘 작동합니다 (테스터에서 콜백 대신 인수 [0]을 사용할 수 있음)
callback.apply(arguments)함수 본문 callbackTester이 두 가지 논쟁 시나리오를 넘어 확장 될 수 있기 때문에 우리가 일반적이라는 정신에있는 한 .
이것은 또한 작동합니다 :
// callback function
function tryMe (param1, param2) {
alert (param1 + " and " + param2);
}
// callback executer
function callbackTester (callback) {
callback();
}
// test function
callbackTester (function() {
tryMe("hello", "goodbye");
});
다른 시나리오 :
// callback function
function tryMe (param1, param2, param3) {
alert (param1 + " and " + param2 + " " + param3);
}
// callback executer
function callbackTester (callback) {
//this is the more obivous scenario as we use callback function
//only when we have some missing value
//get this data from ajax or compute
var extraParam = "this data was missing" ;
//call the callback when we have the data
callback(extraParam);
}
// test function
callbackTester (function(k) {
tryMe("hello", "goodbye", k);
});
if (typeof window[callback] == 'function') window[callback].call(this);
질문이 불분명합니다. 더 간단한 방법으로이 작업을 수행하는 방법을 묻는다면 Function.prototype 의 멤버 인 ECMAScript 5th edition 메소드 .bind ()를 살펴보십시오 . 그것을 사용하면 다음과 같이 할 수 있습니다 :
function tryMe (param1, param2) {
alert (param1 + " and " + param2);
}
function callbackTester (callback) {
callback();
}
callbackTester(tryMe.bind(null, "hello", "goodbye"));
다음 코드를 사용할 수도 있습니다.이 코드는 현재 브라우저에서 사용할 수없는 경우 메소드를 추가합니다.
// From Prototype.js
if (!Function.prototype.bind) { // check if native implementation available
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments),
object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
}
Array.prototype.slice.call(arguments)과 arguments.slice()?
f = function(arg1,arg2){alert(arg1+arg2);}.bind(this,"abc"); f("def") // Gives "abcdef"
특정 수의 매개 변수가있는 코드 이외의 다른 콜백이 있고 추가 매개 변수를 전달하려는 경우 콜백과 래퍼 내부가 추가 매개 변수를 전달할 때 래퍼 함수를 전달할 수 있습니다.
function login(accessedViaPopup) {
//pass FB.login a call back function wrapper that will accept the
//response param and then call my "real" callback with the additional param
FB.login(function(response){
fb_login_callback(response,accessedViaPopup);
});
}
//handles respone from fb login call
function fb_login_callback(response, accessedViaPopup) {
//do stuff
}
여러 매개 변수와 콜백 컨텍스트가 포함 된 질문의 코드 :
function SomeFunction(name) {
this.name = name;
}
function tryMe(param1, param2) {
console.log(this.name + ": " + param1 + " and " + param2);
}
function tryMeMore(param1, param2, param3) {
console.log(this.name + ": " + param1 + " and " + param2 + " and even " + param3);
}
function callbackTester(callback, callbackContext) {
callback.apply(callbackContext, Array.prototype.splice.call(arguments, 2));
}
callbackTester(tryMe, new SomeFunction("context1"), "hello", "goodbye");
callbackTester(tryMeMore, new SomeFunction("context2"), "hello", "goodbye", "hasta la vista");
// context1: hello and goodbye
// context2: hello and goodbye and even hasta la vista
이 간단한 예에서와 같이 커리 기능을 사용하십시오.
const BTN = document.querySelector('button')
const RES = document.querySelector('p')
const changeText = newText => () => {
RES.textContent = newText
}
BTN.addEventListener('click', changeText('Clicked!'))
<button>ClickMe</button>
<p>Not clicked<p>
자체 코드가 아닌 다른 함수에 의해 콜백이 호출되고 추가 매개 변수를 추가하려는 시나리오의 새 버전입니다.
예를 들어 성공 및 오류 콜백이 포함 된 많은 중첩 호출이 있다고 가정 해 봅시다. 이 예제에서는 각도 약속을 사용하지만 콜백이 포함 된 모든 자바 스크립트 코드는 목적에 따라 동일합니다.
someObject.doSomething(param1, function(result1) {
console.log("Got result from doSomething: " + result1);
result.doSomethingElse(param2, function(result2) {
console.log("Got result from doSomethingElse: " + result2);
}, function(error2) {
console.log("Got error from doSomethingElse: " + error2);
});
}, function(error1) {
console.log("Got error from doSomething: " + error1);
});
이제 디버깅 목적으로 오류의 원인을 유지하면서 오류를 기록하는 함수를 정의하여 코드를 정리할 수 있습니다. 다음은 코드를 리팩터링하는 방법입니다.
someObject.doSomething(param1, function (result1) {
console.log("Got result from doSomething: " + result1);
result.doSomethingElse(param2, function (result2) {
console.log("Got result from doSomethingElse: " + result2);
}, handleError.bind(null, "doSomethingElse"));
}, handleError.bind(null, "doSomething"));
/*
* Log errors, capturing the error of a callback and prepending an id
*/
var handleError = function (id, error) {
var id = id || "";
console.log("Got error from " + id + ": " + error);
};
콜백 함수는 콜백 함수 매개 변수 뒤에 오류 매개 변수를 계속 추가합니다.
나는 똑같은 것을 찾고 해결책으로 끝나고 누군가가 이것을 원한다면 간단한 예입니다.
var FA = function(data){
console.log("IN A:"+data)
FC(data,"LastName");
};
var FC = function(data,d2){
console.log("IN C:"+data,d2)
};
var FB = function(data){
console.log("IN B:"+data);
FA(data)
};
FB('FirstName')
또 다른 질문에 게시 여기
콜백을 사용하는 매우 일반적인 Node.js 스타일 예제를 보여 드리겠습니다.
/**
* Function expects these arguments:
* 2 numbers and a callback function(err, result)
*/
var myTest = function(arg1, arg2, callback) {
if (typeof arg1 !== "number") {
return callback('Arg 1 is not a number!', null); // Args: 1)Error, 2)No result
}
if (typeof arg2 !== "number") {
return callback('Arg 2 is not a number!', null); // Args: 1)Error, 2)No result
}
if (arg1 === arg2) {
// Do somethign complex here..
callback(null, 'Actions ended, arg1 was equal to arg2'); // Args: 1)No error, 2)Result
} else if (arg1 > arg2) {
// Do somethign complex here..
callback(null, 'Actions ended, arg1 was > from arg2'); // Args: 1)No error, 2)Result
} else {
// Do somethign else complex here..
callback(null, 'Actions ended, arg1 was < from arg2'); // Args: 1)No error, 2)Result
}
};
/**
* Call it this way:
* Third argument is an anonymous function with 2 args for error and result
*/
myTest(3, 6, function(err, result) {
var resultElement = document.getElementById("my_result");
if (err) {
resultElement.innerHTML = 'Error! ' + err;
resultElement.style.color = "red";
//throw err; // if you want
} else {
resultElement.innerHTML = 'Result: ' + result;
resultElement.style.color = "green";
}
});
그리고 결과를 렌더링 할 HTML :
<div id="my_result">
Result will come here!
</div>
여기에서 재생할 수 있습니다 : https://jsfiddle.net/q8gnvcts/- 예를 들어 숫자 대신 문자열을 전달하십시오 : myTest ( 'some string', 6, function (err, result) .. 결과를보십시오.
이 예제가 콜백 함수의 기본적인 아이디어를 나타 내기 때문에 도움이되기를 바랍니다.
function tryMe(param1, param2) {
console.log(param1 + " and " + param2);
}
function tryMe2(param1) {
console.log(param1);
}
function callbackTester(callback, ...params) {
callback(...params);
}
callbackTester(tryMe, "hello", "goodbye");
callbackTester(tryMe2, "hello");
//Suppose function not taking any parameter means just add the GetAlterConfirmation(function(result) {});
GetAlterConfirmation('test','messageText',function(result) {
alert(result);
}); //Function into document load or any other click event.
function GetAlterConfirmation(titleText, messageText, _callback){
bootbox.confirm({
title: titleText,
message: messageText,
buttons: {
cancel: {
label: '<i class="fa fa-times"></i> Cancel'
},
confirm: {
label: '<i class="fa fa-check"></i> Confirm'
}
},
callback: function (result) {
return _callback(result);
}
});