How to use call(), apply(), bind() in JavaScript?

In JavaScript objects have their own properties and methods. With every function we have three methods: call(), apply() and bind().

Before moving into this concept, first lets have a recap on this in functions. This keyword determines how a function is called (runtime binding).

There is a feature in JavaScript which allows to use the methods of some other objects to another object without redefining them. This way of borrowing is done through call(), apply(), bind() methods.

Call(), apply(), and bind() methods are used to set the this keyword independent of function call. Useful in callbacks.

Call():

Call method sets the this inside function and immediately executes the function. Here you can pass arguments as a list.

Syntax:

function.call(thisArg, arg1, arg2, …)

function – function that needs to be invoked with different this object.

thisArg – value that needs to be replaced with this keyword present inside this function.

arg1,arg2,… – arguments passed for invoking function with this.

Example:
 function test(arg1, arg2){
   console.log(this.num, arg1, arg2); // 100, 10, 20
}

test.call({num: 100}, 10, 20);
Output:

100,10,20

apply()

apply() method binds the this value to the function and executes the function. Here  you can pass argument as an array.

Syntax:
function.apply(this, [argumentsArray])

It returns the result of the function which is invoked by this.

function test(...args){
  console.log(this.num, args);//100, [1,2,3]
}
test.apply({num: 100}, [1,2,3]);
Output:

100, [1,2,3]

Bind()

Bind() function binds this value to the function and returns new function. We need to invoke the returned function separately.

Syntax:
function.bind(this,arg1,arg2,arg3,...)

Bind returns a copy of function with this and arguments.

function test(arg){

 console.log(this.number, arg);

}

let bindedFn = test.bind({number: 1}, "Abaython");

bindedFn();
Output:

1 Abaython

Differences : call() vs apply() vs bind()

call()apply()bind()
DefinitionCall method is a predefined javascript method. With call an object can use a method belonging to another object. Here we supply list of argumentsSimilar to call(). But here we supply array of argumentsBind() helps in creating another function and execute later with new this function
ExecutionAt time of bindingAt time of bindingAt time when we execute the return function
Return?Yes it returns and calls the same function at time of bindingYes it returns and calls the same function at time of bindingYes it returns a new function of copy of function which can be used later.
ParameterList of argumentsArray of argumentsArray and any number of arguments

Key points to note:

  • call: binds the this value, invokes the function, and allows you to pass a list of arguments.
  • apply: binds the this value, invokes the function, and allows you to pass arguments as an array.
  • bind: binds the this value, returns a new function, and allows you to pass in a list of arguments.

How to insert functions to the Functions toolbar of new SAP ALV Display CL_SALV_TABLE

The new ALV Display based on the class CL_SALV_TABLE offers you the possibility to add a new function. It is completely based on the Display mode. In the container mode, the function is added through the functions object. In the complete mode, it is added through the GUI status for the Dynpro.

Adding functions in Container mode:

The Add_function( ) method of the functions object is used to insert functions to the functions toolbar.

DATA lr_functions_list TYPE REF TO cl_salv_functions_list.

lr_functions_list = lr_alv_object->get_functions( ).

lr_functions_list->set_all( ).

lr_functions_list->add_function( name = ‘SUM’  text = ‘SUM’  tooltip = ‘SUM’

position = if_salv_c_function_position=>right_of_salv_function ).

Adding functions in complete mode:

In order to insert a new function in complete mode, copy the GUI status SALV_TABLE_STANDARD in your report.

  1. Open the function group SALV_METADATA_STATUS through the transaction SE80. Right click on the GUI status SALV_TABLE_STANDARD.
  2. Select the copy from the options in order to copy the GUI status.
  3. Give the program name and a name for the copied GUI status in the popup window and finally confirm the entries.
  4. The ALV object must be able to recognize the newly added GUI-status. It is recognized through the set_screen_status method of the ALV object.

Lr_alv_object->set_screen_status( report = ‘ZALV_NEW_FUNCTION’ pfstatus = ‘SALV_TABLE_STANDARD’ set_functions = lr_alv_object->c_functions_all ).

The GUI status can be further adjusted to insert the new function code.

How to add user-defined buttons in SAP ALV TOOLBAR

In this blog post, we discuss about the events of the class CL_GUI_ALV_GRID. The old SAP ALV model provides standard functions such as summing, sorting, search function, Excel Export function etc. It also enables us to add new functions to the SAP ALV toolbar.

The Event TOOLBAR of the class CL_GUI_ALV_GRID is used to add functions to the toolbar of the SAP ALV List.

The following steps are needed in order to add a Button.

  1. Define the Event handler.
  2. Register the Event handler for the event.
  3. Activate the event through the method SET_TOOLBAR_INTERACTIVE()
  1. Definition of Event Handler.

The Event handler is defined in the static method of the class like below.

              CLASS zcl_handler DEFINITION PUBLIC.

   PUBLIC SECTION.

                       CLASS-METHODS st_select_all_method FOR EVENT toolbar

                          OF cl_gui_alv_grid IMPORTING e-object.

               ENDCLASS.

               CLASS zcl_handler IMPLEMENTATION.

                      METHOD st_select_all_method.

                            DATA ls_button_toolbar TYPE stb_button.

                            ls_button_toolbar-function = ‘SELALL’.

                            ls_button_toolbar-icon = icon-select-all.

                            ls_button_toolbar-quickinfo = ‘SELECT ALL function’.

                            INSERT ls_button_toolbar INTO e_object -> mt_toolbar INDEX 2.

                     ENDMETHOD.

               ENDCLASS.

2. Event handler Registration.

The Event handler is registered using the ‘SET HANDLER … FOR … ‘ command.

An example below shows the registration.

SET HANDLER zcl_handler => st_select_all_method FOR lr_gui_alv_grid.

3. Activation of Event.

As a last step, the event must be activated through the method SET_TOOLBAR_INTERACTIVE. This ensures that the event is triggered from ALV. It is the method of the ALV object.

CALL METHOD lr_gui_alv_grid -> set_toolbar_interactive( )

 The event handler registration and event activation code section appears between the ALV table call ‘SET_TABLE_FOR_FIRST_DISPLAY’ and the ‘CALL SCREEN’ of Dynpro call.

 The Event handler uses the parameter e_object in order to insert the button entry into the internal table mt_toolbar. The parameter E_OBJECT is of type CL_ALV_EVENT_TOOLBAR_SET.