Functions that take or return other functions In Hindi

Functions that take or return other functions In Hindi

जावास्क्रिप्ट प्रोग्रामिंग में यूजर क्रिएटेड फ़ंक्शन को अन्य फ़ंक्शन में आर्गुमेंट के रूप में एक्चुअल और फॉर्मल आर्गुमेंट के बिच में पास किया जाता है, इसके साथ ही अन्य यूजर क्रिएटेड प्रोग्राम फ़ंक्शन भी रिटर्न किए जा सकते हैं। फंक्शन रिटर्न जावास्क्रिप्ट प्रोग्रामिंग की एक पावरफुल फीचर्स है. जहा फंक्शन फीचर्स जावास्क्रिप्ट प्रोग्रामर को हाई-आर्डर, फंक्शन क्लोज़र फीचर्स और कॉलबैक फ़ंक्शन और फ़ंक्शनल प्रोग्रामिंग जैसे फीचर्स को उपयोग करने में हेल्पफुल बनाता है।

Functions that take or return other functions In Hindi

So, let’s understand the process of passing arguments to a function in JavaScript programming.

JavaScript functions that deal with other functions as arguments.

जावास्क्रिप्ट प्रोग्राम में यूजर डिफाइन फ़ंक्शन अन्य प्रोग्राम फ़ंक्शन को आर्गुमेंट के रूप में डील करते हैं। यह फीचर्स ज्यादातर कॉलबैक फ़ंक्शन में पाया जाता है, जहाँ यूजर डिक्लेअर एक फ़ंक्शन दूसरे फ़ंक्शन द्वारा अपना प्रोसेस कम्पलीट करने के बाद एक्सेक्यूट होता है।

Passing function as argument in JavaScript callback example.

function message(compname, callback) {

  console.log(`Vcanhelpsu, ${compname}`);

  callback();

}

function youcanjoin() {

  console.log(“Get your free digital resources”);

}

message(“Harry”, youcanjoin);

Passing function as argument in JavaScript callback explained.

इस एक्साम्प्ल में, message फ़ंक्शन एक नाम और एक कॉलबैक फ़ंक्शन को इनपुट आर्गुमेंट के रूप में इनपुट करता है। यह कंपनी नाम डिस्प्ले करने के बाद callback() फंक्शन को कॉल करता है। और Get your free digital resources मैसेज को कॉलबैक फ़ंक्शन के रूप में पास करता है।

Why use function as argument in JavaScript program.

  • Asynchronous programming – जावास्क्रिप्ट प्रोग्रामिंग में एसिंक्रोनस एक बेसिक कांसेप्ट है, जहाँ किसी प्रोग्राम में एक फ़ंक्शन को कॉलबैक फंक्शन के रूप में पास किया जा सकता है, जिससे की मौजूदा प्रोग्राम में एसिंक्रोनस टास्क कम्पलीट होने के बाद उन्हें एक्सेक्यूट किया जा सके।
  • Custom behavior – जावास्क्रिप्ट प्रोग्राम में यूजर क्रिएटेड कस्टम फ़ंक्शन को आर्गुमेंट के रूप में पास कर परमिशन प्रोवाइड करने से कॉलिंग फ़ंक्शन अपने बिहैवियर को कस्टमाइज कर सकता है।

Functions in JavaScript programs that return other functions.

जावास्क्रिप्ट प्रोग्राम में एक फ़ंक्शन किसी एक अन्य फ़ंक्शन वैल्यू को भी रिटर्न कर सकता है। जहा प्रोग्रामर को फ़ंक्शन फ़ैक्टरी, क्लोज़र और सिक्योर और प्राइवेट  फ़ंक्शन क्रिएट करने में हेल्प करते है।

Example of a function returning another function value.

function product(fact) {

  return function(integer) {

    return integer * fact;

  };

}

const single = product(1);  // here single is now a function that single integer value

console.log(single(7));         // Result – 7

const double = product(2);  // here double now a function that double integer value

console.log(double(10));         // Output: 15

Explanation of function returning another function value.

इस प्रोग्राम में प्रोडक्ट फ़ंक्शन एक प्रोडक्ट वैल्यू को इनपुट करता है, और एक न्यू फ़ंक्शन वैल्यू को रिटर्न करता है. जो फंक्शन में आर्गुमेंट को उस प्रोडक्ट से मल्टीप्लय करता है। जहा रिटर्न किया गया फ़ंक्शन, क्लोजर फंक्शन फीचर्स के कारण, प्रोडक्ट को “होल्ड” रखता है, इस कारण यहाँ हम सिंगल और डबल जैसे कई फ़ंक्शन क्रिएट कर सकते हैं, जो अलग-अलग तरह से बिहेव करते हैं।

Why function values are returned in JavaScript programs.

  • Currying – यह फीचर्स जावास्क्रिप्ट प्रोग्रामर को आर्गुमेंट को आंशिक रूप से अप्लाई करके फ़ंक्शन के स्पेसिफिक वर्शन क्रिएट करने में हेल्प करता है।
  • Function Factory – जावास्क्रिप्ट प्रोग्रामर को फंक्शन प्रोग्राम कोड को रिपीट किए बिना मल्टीप्ल टास्कस के लिए कस्टमाइज फ़ंक्शन क्रिएट कर सकते हैं।

Example of higher-order function in JavaScript.

जावास्क्रिप्ट प्रोग्राम फंक्शन में एक हायर-आर्डर फ़ंक्शन वह होता है, जो किसी प्रोग्राम में या तो एक या अधिक फ़ंक्शन को वैल्यू आर्गुमेंट के रूप में इनपुट करता है, जहा यह मौजूदा प्रोग्राम में एक फ़ंक्शन वैल्यू को रिटर्न करता है, या दोनों फंक्शन वैल्यू को।

Higher-order function examples in JavaScript.

function selectcourse(programming) {

  return function(programmingname) {

    if (programming === “prog1”) {

      return “Javascript, ” + programmingname;

    } else if (programming === “prog2”) {

      return “Python, ” + programmingname;

    } else if (programming === “prog3”) {

      return “Html, ” + programmingname;

    }

  };

}

const selectjavascript = selectcourse(“prog1”);

const selectpython = selectcourse(“prog2”);

const selecthtml = selectcourse(“prog3”);

console.log(selectjavascript(“Harry”));  // Result – Javascript, Harry

console.log(selectpython(“Bhavishi”)); // Result – Python, Bhavishi

console.log(selecthtml(“Siddhi”)); // Result – Html, Siddhi

Explanation of Higher-order function.

इस प्रोग्राम में selectcourse() फ़ंक्शन प्रोग्रामिंग को आर्गुमेंट के आधार पर एक नया फ़ंक्शन वैल्यू को रिटर्न करता है। जहा रिटर्न किए गए फ़ंक्शन का उपयोग विभिन्न प्रोग्रामिंग लैंग्वेज कोर्स में लोगों का सिलेक्शन प्रोवाइड करने में किया जा सकता है। जहा, selectcourse() एक यूजर क्रिएटेड हायर-आर्डर वाला फ़ंक्शन है. क्योंकि यह एक फ़ंक्शन वैल्यू को रिटर्न करता है।

Practical Uses of JavaScript Functions.

जावास्क्रिप्ट प्रोग्राम में फ़ंक्शन को आर्गुमेंट के रूप में उपयोग करने वाले इवेंट हैंडलर

ऐसे फ़ंक्शन का एक सामान्य रियलटाइम एक्साम्प्ल है. जो किसी प्रोग्राम में अन्य फ़ंक्शन वैल्यू को इनपुट करते या रिटर्न करते हैं, जावास्क्रिप्ट प्रोग्राम में इवेंट हैंडलर फीचर्स के साथ काम करते समय यह होता है. जैसे कि, setTimeout या DOM इवेंट का उपयोग करते समय इवेंट हैंडलर फीचर्स को अप्लाई किया जा सकता है।

Example of an event handler using a callback function in a JavaScript program.

function pressbutton(callback) {

  console.log(“Button pressed”);

  callback();

}

function displayinfo() {

  console.log(“You press the button”);

}

pressbutton(displayinfo);

Explanation of event handler using a callback function.

इस प्रोग्राम एक्साम्प्ल में, pressbutton एक फ़ंक्शन (displayinfo) को एक आर्गुमेंट के रूप में इनपुट करता है, और बटन प्रेस जैसे इवेंट हैंडलर कंडीशन को मैनेज का अनुकरण करने के लिए “बटन प्रेस किया गया!” मैसेज क लॉग करने के बाद उसे कालबैक फंशन में कॉल करता है।

Example of a function returning a function to handle customization in JavaScript.

So, let’s understand the use of function returns and closures to create a customizable logging function in a JavaScript program

Example of a JavaScript customizable logging function.

function logfile(prefix) {

  return function(info) {

    console.log(`${prefix} – ${info}`);

  };

}

const infoLogfile = logfile(“INFORMATION”);

const warnLogfile = logfile(“WARNING”);

const deleteLogfile = logfile(“DELETE”);

infoLogfile(“This is a info logfile information”);   // Result – INFORMATION – This is a info logfile information

warnLogfile(“This is a info warnlogfile information”); // Result – WARNING – This is a info warnfile information

deleteLogfile(“This is a info deletelogfile information”); // Result – DELETE – This is a info deletelogfile information

Explanation of JavaScript customizable logging function.

यहाँ logfile दिए गए प्रीफिक्स के साथ अनुकूलित एक नया लॉगिंग फ़ंक्शन वैल्यू को रिटर्न करता है। जहा प्रत्येक रिटर्न किया गया logfile फ़ंक्शन अपने स्पेसिफिक प्रीफिक्स वैल्यू को होल्ड रखता है, और लोग फाइल मैसेज को लॉग करने के लिए इसका उपयोग करता है।

Example of function returning function with closure in JavaScript program.

जावास्क्रिप्ट प्रोग्राम में क्लोजर कांसेप्ट इंटरनल फ़ंक्शन को अपने एक्सटर्नल फ़ंक्शन से डिक्लेअर फंक्शन वेरिएबल्स को होल्ड रखने और उन्हें एक्सेस करने की परमिशन प्रोवाइड करते है।

Example of function returning function with access to external variables.

function developtester() {

  let tester = 1;  // here tester declare as a private variable

  return {

    increment: function() {

      tester++;

      console.log(tester);

    },

    decrement: function() {

      tester–;

      console.log(tester);

    },

    gettester: function() {

      return tester;

    }

  };

}

const tester = developtester();

Explanation of function returning function with access to external variables.

यहाँ developtester () एक ऑब्जेक्ट वैल्यू को रिटर्न करता है, जिसमें ऐसे प्रोग्राम फ़ंक्शन होते हैं, जो टेस्टर वेरिएबल को मेन्युप्लेट और उसको एक्सेस कर सकते हैं। जहा टेस्टर वेरिएबल ऑब्जेक्ट में एक्सटर्नाली एक्सेसिबल नहीं है, लेकिन फंक्शन ऑब्जेक्ट के अंदर के फ़ंक्शन क्लोजर के माध्यम से उसको एक्सेस कर सकते हैं।

Conclusion of Functions that take or return other functions in JavaScript.

  • जावास्क्रिप्ट प्रोग्राम में ऐसे यूजर डिक्लेअर फ़ंक्शन होते है, जो अन्य फ़ंक्शन को आर्गुमेंट के रूप में इनपुट करते हैं, यह फीचर्स जावास्क्रिप्ट प्रोग्रामर को प्रोग्राम में कॉलबैक फ़ंक्शन कालबैक और एसिंक्रोनस प्रोग्रामिंग ऑपरेशन की परमिशन प्रोवाइड करते है।
  • फंक्शन जो दूसरे फंक्शन को रिटर्न करते है, जो किसी जावास्क्रिप्ट प्रोग्राम में ऐसे फ़ंक्शन होते है, जो अन्य फ़ंक्शन वैल्यू या फंक्शन आर्गुमेंट को रिटर्न करते हैं, ये मौजूदा प्रोग्राम में कस्टमाइज बिहेवियर या फ़ंक्शन फ़ैक्टरी क्रिएट करते हैं, और ज्यादातर फंक्शन में क्लोजर में अप्लाई किए जाते हैं।
  • हायर-आर्डर फ़ंक्शन जावास्क्रिप्ट प्रोग्राम में ऐसे फ़ंक्शन यूजर जनरेटेड फंक्शन होते है, जो मौजूदा प्रोग्राम में या तो अन्य फ़ंक्शन को आर्गुमेंट के रूप इनपुट करते हैं, या फ़ंक्शन वैल्यू को रिटर्न करते हैं, या दोनों कंडीशन को फॉलो करते है।

Leave a Reply