From 55c27bbc466dc5e1aaaab73ca0cda82ec95ab4fd Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 17 Apr 2022 11:02:16 +0000 Subject: [PATCH] Restyled by prettier-markdown --- .../passing-arguments-to-a-callback-in-js.md | 81 ++++++++++--------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/src/pages/blog/passing-arguments-to-a-callback-in-js.md b/src/pages/blog/passing-arguments-to-a-callback-in-js.md index 6134b054b5..f29573d396 100644 --- a/src/pages/blog/passing-arguments-to-a-callback-in-js.md +++ b/src/pages/blog/passing-arguments-to-a-callback-in-js.md @@ -17,42 +17,45 @@ related_posts: - src/pages/blog/data-structures-algorithms-resources.md cmseditable: true --- -By default you cannot pass arguments to a callback function. For example: - -```js -function callback() { - console.log('Hi human'); -} - -document.getElementById('someelem').addEventListener('click', callback); -``` - -You can take advantage of the closure scope in Javascript to pass arguments to callback functions. Check this example: - -```js -function callback(a, b) { - return function () { - console.log('sum = ', a + b); - }; -} - -var x = 1, - y = 2; -document.getElementById('someelem').addEventListener('click', callback(x, y)); -``` - -### What are closures? - -Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created. [Check MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) to learn more. - -So this way the arguments `x` and `y` are in scope of the callback function when it is called. - -Another method to do this is using the `bind` method. For example: - -```js -var alertText = function (text) { - alert(text); -}; - -document.getElementById('someelem').addEventListener('click', alertText.bind(this, 'hello')); -``` \ No newline at end of file + +By default you cannot pass arguments to a callback function. For example: + +```js +function callback() { + console.log("Hi human"); +} + +document.getElementById("someelem").addEventListener("click", callback); +``` + +You can take advantage of the closure scope in Javascript to pass arguments to callback functions. Check this example: + +```js +function callback(a, b) { + return function () { + console.log("sum = ", a + b); + }; +} + +var x = 1, + y = 2; +document.getElementById("someelem").addEventListener("click", callback(x, y)); +``` + +### What are closures? + +Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created. [Check MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) to learn more. + +So this way the arguments `x` and `y` are in scope of the callback function when it is called. + +Another method to do this is using the `bind` method. For example: + +```js +var alertText = function (text) { + alert(text); +}; + +document + .getElementById("someelem") + .addEventListener("click", alertText.bind(this, "hello")); +```