Call Functions with Context

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to call functions with context using a closure in JavaScript. We will learn how to pass a key and a set of arguments to a function, and then call it with the given context. Through practical examples, we will see how this technique can be used to simplify code and make it more readable.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/async_prog("`Asynchronous Programming`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") javascript/ToolsandEnvironmentGroup -.-> javascript/debugging("`Debugging`") subgraph Lab Skills javascript/variables -.-> lab-28185{{"`Call Functions with Context`"}} javascript/data_types -.-> lab-28185{{"`Call Functions with Context`"}} javascript/arith_ops -.-> lab-28185{{"`Call Functions with Context`"}} javascript/comp_ops -.-> lab-28185{{"`Call Functions with Context`"}} javascript/array_methods -.-> lab-28185{{"`Call Functions with Context`"}} javascript/async_prog -.-> lab-28185{{"`Call Functions with Context`"}} javascript/higher_funcs -.-> lab-28185{{"`Call Functions with Context`"}} javascript/spread_rest -.-> lab-28185{{"`Call Functions with Context`"}} javascript/debugging -.-> lab-28185{{"`Call Functions with Context`"}} end

How to Call Functions with Context in JavaScript

To execute code in Node.js, open the Terminal/SSH and type node. If you want to call a function with a specific context and a set of arguments in JavaScript, you can use a closure. Here's how you can do it:

  1. Define a function called call that takes a key and a set of args as parameters and returns a new function that takes a context parameter.
const call =
  (key, ...args) =>
  (context) =>
    context[key](...args);
  1. Use the call function to call the map function on an array of numbers. In this example, the map function doubles each number in the array.
Promise.resolve([1, 2, 3])
  .then(call("map", (x) => 2 * x))
  .then(console.log); // [ 2, 4, 6 ]
  1. You can also bind the call function to a specific key, such as map, and use it to call the map function on an array of numbers.
const map = call.bind(null, "map");
Promise.resolve([1, 2, 3])
  .then(map((x) => 2 * x))
  .then(console.log); // [ 2, 4, 6 ]

By using the call function, you can easily call any function with a specific context and set of arguments.

Summary

Congratulations! You have completed the Call Functions With Context lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like