0

I am trying to pass in an array of functions as a parameter to function x then execute them within the function x. I will also somehow pass in parameters but some of the parameters are only initialised within function x.

Some functions include things like:

_showData(data,type);
console.log(data);
$('#loading').remove();

Here is a sample:

// Called somewhere else 
runFunctions([$('.dashboard').remove, $('.screen-loading').remove]);

var runFunctions = function(functions){
  // do some things
  for (var i = 0; i < functions.length; i++){
     functions[i]();
}

Any ideas?

EDIT: Sorry I just realised the program doesn't know what the objects are because I'm changing scope with an ajax call.

var runFunctions = function(functions){
  $.ajax({
    method: "POST",
    url: "php/database.php",
    dataType: "JSON",
    data: {type:type},
    success: function(data, type){
      for (var i = 0; i < functions.length; i++){
        functions[i]();
      }
    }
  })
}

What about this:

  _accessDatabase( 
    function(onSuccess){
      $('.dashboard').remove();
      var type = 'home';
      _showData(data,type); // it doesn't know what data is, how can I pass it through?
      $('.screen-loading').remove();
    }
  );


var _accessDatabase = function(onSuccess){
  $.ajax({
    method: "POST",
    url: "php/database.php",
    dataType: "JSON",
    data: {},
    success: function(data){
      onSuccess(data); 
    }
  })
}

I want to pass through var data to the onSuccess function, how can I do this?

Solved with:

var _request_successful = function onSuccess (data){
  console.log("running onSuccess");
  $('.dashboard').remove();
  var type = 'home';
  _showData(data,type);
  $('.screen-loading').remove();
}

_accessDatabase(_request_successful);


var _accessDatabase = function(onSuccess){
  $.ajax({
    method: "POST",
    url: "php/database.php",
    dataType: "JSON",
    data: {},
    success: function(data){
      onSuccess(data); 
    }
  })
}   
ChickenFeet
  • 2,653
  • 22
  • 26

1 Answers1

1

The problem with this code is that the functions that you're calling within the forLoop aren't bound to anything. Take this instead.

// Called somewhere else 
runFunctions([
  $('.dashboard').remove.bind($('.dashboard'))
, $('.screen-loading').remove.bind($('.screen-loading'))
]);

function runFunctions(functions){
  // do some things
  for (var i = 0; i < functions.length; i++){
     console.log("running")
     functions[i]();
  }
}

What you could do instead is this:

function call(method, objs) {
  objs.forEach(function (obj) {
     obj[method]()
  })
}
call('remove', [$('.dashboard'), $('.screen-loading')])

Here's a working fiddle: https://jsfiddle.net/ogfgocp4/

To explain a bit how it works, I don't know exactly the internal of JavaScript, but when you do: $('.dashboard').remove, it will return you the remove function. If you call it immediatly, it will be bound to the object which give you the method. If you affect it to something else, then it will be bound to the object it's being called from.

Here's a small snippet of code that explains it well I guess.

var obj = {
    fun: function () {
    console.log(this)
  }
}
var fun2 = {
    a: 1
}

//this -> obj
obj.fun()

// this -> window
fun = obj.fun
fun()

// this -> fun2
fun2.fun = obj.fun
fun2.fun()

When you call obj.fun, this will be the object obj. When you affect the method to a var, this then become window as it is the default object in this scope. Then if we finally bind the function to object fun2 and call it immediatly, this is now the object fun2.

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99