किसी अन्य फ़ंक्शन में पैरामीटर के रूप में कॉलबैक कैसे पास करें


83

मैं अजाक्स और कॉलबैक कार्यों के लिए नया हूं, कृपया मुझे माफ कर दें अगर मुझे अवधारणा गलत मिलती है।

समस्या: क्या मैं कॉलबैक को निष्पादित करने वाले किसी अन्य फ़ंक्शन के पैरामीटर के रूप में कॉलबैकफंक्शन भेज सकता हूं ?

function firstFunction(){
    //some code

    //a callback function is written for $.post() to execute
    secondFunction("var1","var2",callbackfunction);
}

function secondFunction(var1, var2, callbackfunction) {
    params={}
    if (event != null) params = event + '&' + $(form).serialize();

    // $.post() will execute the callback function
    $.post(form.action,params, callbackfunction);
}

जवाबों:


125

हाँ। फ़ंक्शन संदर्भ किसी भी अन्य ऑब्जेक्ट संदर्भ की तरह हैं, आप उन्हें अपने दिल की सामग्री के आसपास पारित कर सकते हैं।

यहाँ एक और अधिक ठोस उदाहरण दिया गया है:

function foo() {
    console.log("Hello from foo!");
}

function caller(f) {
    // Call the given function
    f();
}

function indirectCaller(f) {
    // Call `caller`, who will in turn call `f`
    caller(f);
}

// Do it
indirectCaller(foo); // alerts "Hello from foo!"

आप इसके लिए तर्क भी दे सकते हैं foo:

function foo(a, b) {
    console.log(a + " + " + b + " = " + (a + b));
}

function caller(f, v1, v2) {
    // Call the given function
    f(v1, v2);
}

function indirectCaller(f, v1, v2) {
    // Call `caller`, who will in turn call `f`
    caller(f, v1, v2);
}

// Do it
indirectCaller(foo, 1, 2); // alerts "1 + 2 = 3"




2

हां, निश्चित रूप से, फ़ंक्शन ऑब्जेक्ट हैं और पारित किए जा सकते हैं, लेकिन निश्चित रूप से आपको इसे घोषित करना होगा:

function firstFunction(){
    //some code
    var callbackfunction = function(data){
       //do something with the data returned from the ajax request
     }
    //a callback function is written for $.post() to execute
    secondFunction("var1","var2",callbackfunction);
}

एक दिलचस्प बात यह है कि आपके कॉलबैक फ़ंक्शन की पहुंच हर उस चर तक भी होती है, जिसे आपने फ़र्स्टफ़ंक्शन () में घोषित किया होगा (जावास्क्रिप्ट में वेरिएबल में स्थानीय गुंजाइश होती है)।


0

इसके लिए उदाहरण CoffeeScript:

test = (str, callback) ->
  data = "Input values"
  $.ajax
    type: "post"
    url: "http://www.mydomain.com/ajaxscript"
    data: data
    success: callback

test (data, textStatus, xhr) ->
  alert data + "\t" + textStatus

->जावास्क्रिप्ट में क्या मतलब है? @ कुछ नहीं-यहाँ
shenkwen

->बस एक सामान्य कार्य है। var test = function(str, callback) { ajax call }
बैरीमोड

@shenkwen एक पतला तीर -> कॉफीस्क्रिप्ट सिंटैक्स है, जावास्क्रिप्ट नहीं है, और केवल जावास्क्रिप्ट में संकलित होने पर सामान्य जावास्क्रिप्ट फ़ंक्शन का मतलब है। जावास्क्रिप्ट में एक समान एरो फंक्शन w3schools.com/Js/js_arrow_function.asp
ब्रायन

0

आप इस तरह से जावास्क्रिप्ट कॉलबाक का उपयोग कर सकते हैं:

var a;

function function1(callback) {
 console.log("First comeplete");
 a = "Some value";
 callback();
}
function function2(){
 console.log("Second comeplete:", a);
}


function1(function2);

या जावा स्क्रिप्ट वादा:

let promise = new Promise(function(resolve, reject) { 
  // do function1 job
  let a = "Your assign value"
  resolve(a);
});

promise.then(             

function(a) {
 // do function2 job with function1 return value;
 console.log("Second comeplete:", a);
},
function(error) { 
 console.log("Error found");
});
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.