article

callbacks

[WHAT]

  1. ] a callback ( also known as "higher order function") is a simply a function that is passed to another function as an arguement and executed inside the function that it is passed into 
  2. ] the passed function can be passed by value(byVal) or by Reference(byRef) as an arguement to another function,
    1. by value means your are passing the entire function definition
    2. by reference means you are passing the name of a function that has
  3. ] when the first function completes execution, the callback function will begin execution
  4. ] essentially a "pattern", derived from a programming paradigm called functional programming. At a simple and fundamental level, functional programming is the use of functions as arguments. 

[WHY]

  1. ] In JavaScript, functions are first-class objects, which means functions can be used in a first-class manner like objects, since they are in fact objects themselves: 
    1. ] they can be passed as arguments to functions
    2. ] they can be “stored in variables, 
    3. ] they can be created within functions, and 
    4. ] they can be returned from functions” [1].

[WHERE]

  1. ] you want to execute a 2nd(or more) functions after your first function is called

[WHEN]

[EXAMPLE]

  1. ] lets say that we have 2 functions, 
    1. ] function goToStore()
    2. ] function getDirectionsToStore()
  2. ] in order for us to use the 2nd function we need to know the returned value of the first function
    1. ] we will use a callback to perform/execute the 2nd function after the first function has completed
  3. ] pass by val - passes the entire function body to the calling function
    1. ] function goToStore(getDirectionsToStore()
      {
          var storeId = input("what store do you want to go to? Enter the store id"); 
          return storeId;
      }
      ) 
  4. ] pass by reference - passes just the 'name of' the function, (and any arguements) 
    1. ] function goToStore(getDirectionsToStore());
  5. ] ? when to use pass by value VS pass by reference

[HOW-TO]

  1. ] implementing callback functions is as easy as passing regular variables as arguments to functions

[REFERENCE]

  1. http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

 


  1. ] getDirectionsToStore(goToStore)
  2. ] buyGroceries(groceryList)
  3. ] goHome(getDirections)
  4. ] cookDinner(recipe)
  5. ] eat dinner()

 


function goToStore(storeId){
   return directions = select * from directions where start = home and end = storeId ;
}


function getDirectionsToStore(){
  var storeId = prompt("what store do you want to go to? Enter the store id");
  return storeId ;
}

 

Details Photos Edit more

Details

ID: 1765

NAME: callback

DESCRIPTION: ] understanding the callback, what it is, where and when to use it, how to create one,

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: 0

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and