How does the "deliver_order" function retrieve the value of the name parameter?

function take_order(name, callback1) {
  console.log("order has been taken.");
  callback1(name);
}

function prosess_order(name, callback2) {
  console.log(`prosesing order for ${name}.`);
  callback2(name);
}

function deliver_order(name) {
  console.log(`deliver to ${name}.`);
}

take_order("Mohiul", function (name) {
  prosess_order(name, function (name) {
    deliver_order(name);
  });
});
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
</html>

How does the "deliver_order" function receive the value of the name variable? How is it able to print with the name value? Please provide an explanation regarding this process. (This code snippet was obtained from a tutorial)

Answer №1

Here is a breakdown of the process:

The take_order function is invoked with the argument "Mohiul"

Within the take_order function, an anonymous function is called

function (name) {
  prosess_order(name, function (name) {
    deliver_order(name);
  });
}

This anonymous function receives the name value from take_order and passes it to the prosess_order function.

The prosess_order function then calls another anonymous function with the name argument

function (name) {
    deliver_order(name);
  }

The second anonymous function invokes the deliver_order function with the name variable

This demonstrates how the deliver_order function obtains the name value

Understanding closures in JavaScript is essential for grasping these concepts fully

For more information on closures in JavaScript, check out this article

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I dynamically update the content of the right side of the side navbar by clicking on navbar links?

I have a side-navigation bar with content on the right side. How can I display specific content on the right side when clicking on a navigation link? For example, only show the name when clicking on the name link and only show the password field when click ...

Having trouble accessing the property 'top' of an undefined object in your Ruby on Rails project?

After thoroughly reviewing all the similar threads on SO regarding this error, none of them provided a solution that worked for me. Here is the flow of events: Go to the new Customer page, fill out the required fields, and click save Invoke a method in ...

VueJS Component has trouble refreshing the DOM content following an AJAX Promise

I've encountered issues updating data in my Vue application after an AJAX callback. I have previously resolved similar problems by using Vue.set, but for some reason, it's not working for me today. The only difference is that I am calling a serv ...

Dividing an AngularJS module across multiple iFrames on a single webpage

Currently, I am working on a web application that consists of 1 module, 5 pages, and 5 controllers. Each HTML page declares the same ng-app. These pages are loaded within widgets on a web portal, meaning each page is loaded within an iFrame in the portal. ...

When initiating a new browser window with JQuery, some data is not being transmitted

Encountering an issue with the windows.open(); method. Tech Stack: Asp.netMVC, C#, Razor Problem: jQuery When the function is triggered, it should send parameters to the Actioid and taskid controllers. However, I am facing an issue where only Actioid and ...

Is the form being submitted with the href attribute?

Does this code ensure security? <form id="form-id"> <input type='text' name='name'> <input type='submit' value='Go' name='Go'/></form> <a href="#" onclick="docume ...

Using async setState in React has no impact

I am working on an app.jsx that makes requests to two different APIs named API_A and API_B. From these two APIs, I receive data_A and data_B which I then merge into the info state for rendering the UI. Here is a snippet of my code: const [info, setInfo] ...

Tips for choosing a particular value in an HTML <script> query

Can someone please help me figure out how to select specific values in a form using a query? I'm trying to extract certain values like Hello and Lorem ipsum..., as well as Hi and Nullam fringilla... from the Content.join(''); section. I apo ...

Angular: Navigating to another URL causes the page to revert back to the default route

Currently, I am working with Angular 1.3.10, ui-router 0.2.10, and Express 4.14.0 to develop a Reddit-style application, and I'm facing some issues with routing. The simplified version of my Express routes: router.get('/api/home', function ...

Swap out the div block with a new one

I am currently facing an issue with my JavaScript code. The code is supposed to remove block1 and replace it with block2 when an onclick function is triggered. function buyerclick() { div = document.getElementById('block2'); div.style.displa ...

Troubleshooting Problem with JQuery in the YouTube Player API

Having some difficulty parsing YouTube video ids to a function that plays them in an embedded player using a combination of JavaScript and jQuery with the YouTube Data and Player APIs. Below is my code: <html> <head> <script src="//aj ...

jQuery Slider not appearing on the page

I am attempting to incorporate the Basic jQuery Slider into my website. Below is the code I have used: For HTML file: <div id="slides"> <ul class="bjqs"> <li><a href="somelink"><img src="someim ...

Marionette's Take on the Undead: Zombie Perspectives

Error in JS Console: Uncaught ViewDestroyedError: View (cid: "view351") has already been destroyed and cannot be used. backbone.marionette.js?body=1:1715 Code Snippet: initialize: (options) -> HWAs = @model.get('homework_assignments') @ ...

How can we leverage Shared and Core modules alongside Feature modules in Angular development?

When developing my Angular application, I have adopted a specific architecture approach and included a shared.module.ts file in the shared folder. While leveraging lazy-loading in my app, I find myself puzzled about the necessary imports, declarations, and ...

Function defined as an AngularJS component

I am facing an issue where my component is not initializing when I create it with a function that returns a component object. Can someone please help me understand the difference between these two situations? Html: <div ng-app="demoApp"> <navb ...

Can you guide me on how to export a named function using module.exports?

I have a script file for my discord bot that includes a specific command and a function with parsing logic that I want to reuse in my main index.js // File: ./commands/scrumPrompt.js // The function const extractDeets = function (f, scrum) { let items ...

React - CSS Transition resembling a flip of a book page

As I delve into more advanced topics in my journey of learning React and Front Web Dev, I discovered the ReactCSSTransitionGroup but learned that it is no longer maintained so we now use CSSTransitionGroup. I decided to create a small side project to expe ...

ERROR: JSON - Cannot access property of undefined

My goal is to extract JSON values and create variables from them, but I keep encountering an error. Even when the JSON array is filled, I still face issues. var tempArray = []; $("#sth td").each(function(){ $this = $(this); tempArray.push({"COLO ...

Generating hierarchical structures from div elements

Looking for guidance on how to parse a HTML page like the one below and create a hierarchical Javascript object or JSON. Any assistance would be much appreciated. <div class="t"> <div> <div class="c"> <input t ...

Updating Jqplot display upon ajax call completion

Currently, I have a Jqplot set up using the AJAX JSON Data Renderer and it's functioning properly. There is a button on the page where users can input new values (updated through ajax) which are then stored in the same DB as the json data source. Whe ...