JavaScript Vocabulary - SEIR-59/course-wiki GitHub Wiki

Feel free to add to and edit this page with any new words as they come up. This is intended to help with reviewing for interviews related to JavaScript.

Formatting Notes*: Please keep this page in ALPHABETICAL order, use Heading 2 (h2) for the word titles, and source your information if it isn't your own words. If it is your own words, be sure it's right, but hey, thats what a wiki is for, right? Be sure and leverage the "Preview" tab instead of saving to check your formatting so the edits are clear.

*If you aren't familiar with markdown editing, click the ? in the toolbar above or use buttons provided for basic formatting

Callback function

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

Here is a quick example:

function greeting(name) {
  alert('Hello ' + name);
  }

function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}

processUserInput(greeting);

The above example is a synchronous callback, as it is executed immediately.

Note, however, that callbacks are often used to continue code execution after an asynchronous operation has completed — these are called asynchronous callbacks. A good example is the callback functions executed inside a .then() block chained onto the end of a promise after that promise fulfills or rejects. This structure is used in many modern web APIs, such as fetch().

Closure

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

To use a closure, define a function inside another function and expose it. To expose a function, return it or pass it to another function.

The inner function will have access to the variables in the outer function scope, even after the outer function has returned.

Among other things, closures are commonly used to give objects data privacy.

Example

const getSecret = (secret) => {
  return {
    get: () => secret
  };
};

In the example above, the .get() method is defined inside the scope of getSecret(), which gives it access to any variables from getSecret(), and makes it a privileged method.

For (a lot) more information, click through the source for this definition.


Sources

Closure: https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36#.6xq65f6f5

To use a closure, define a function inside another function and expose it. To expose a function, return it or pass it to another function.

The inner function will have access to the variables in the outer function scope, even after the outer function has returned.

Among other things, closures are commonly used to give objects data privacy.

Example

const getSecret = (secret) => {
  return {
    get: () => secret
  };
};

In the example above, the .get() method is defined inside the scope of getSecret(), which gives it access to any variables from getSecret(), and makes it a privileged method.

For (a lot) more information, click through the source for this definition.


Sources

Callback: https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

Closure: https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36#.6xq65f6f5

Invoking a Function

Invoking a function is when we write out a function with its clappers so that it can execute right away.

###Example:


//define function
function sum(a,b) {
  return a + b;
}

// now to execute it we will invoke by passing arguments. if the function does not have parameters then we don't need to add arguments:
sum(9,3) // 12