Understand context statement given function javascript [duplicate]

1

I have this particular function declaration:

datasetCSR = this.execECM(function () {
    var co = this.DatasetFactory.createConstraint("ibv", instance, instance, this.ConstraintType.MUST);
    return this.DatasetFactory.getDataset("csr", null, [co], null);
});

execECM is a function.

I would like to know the theory and context of this type of function statement.

When datasetCSR is run it automatically runs exeECM?

    
asked by anonymous 14.09.2015 / 15:26

1 answer

1

datasetCSR is a variable that when declared executes and receives the return value of the execECM method.

Given the object definition as:

var obj = {
  execECM: function ( callback ) 
  {
    // será passado como argumento para esse método
    // uma função como callback 
    // você poderá invocá-la a qualquer momento dentro do contexto
    // de execECM
    return callback() + 5;
  },
  myCustomFunction: function ( value ) 
  {
    // declarando datasetCSR e associando seu valor ao valor de retorno
    // do método execECM
    var
      datasetCSR = this.execECM( function () {
        return value;
      } );
    console.log( datasetCSR );
  }
};

The execECM method receives a function such as callback to use at some point in the scope of its declaration.

  

A callback function, also known as a higher-order function, is a function that is passed to another function (let's call this other function "otherFunction") as a parameter, and the callback function is called (or executed) inside the otherFunction.

Understand JavaScript Callback Functions and Use Them

Using the previously declared object, we will have:

obj.myCustomFunction( 10 ); // 15
obj.myCustomFunction( 1 ); // 6
obj.execECM( 10 ) // Uncaught TypeError: callback is not a function(…)
var calls = function () 
{
  return 100;
};
obj.execECM( calls ); // 105
    
14.09.2015 / 18:48