Javascript - Call method

Chrome Dev Tool Source Debugger Scope

Definition

Calls (executes) a function and sets its this to the provided value

if the function has not be binded

, arguments can be passed as they are.

call(this) gives the ability to pass the current context (scope)

Example

Hello World

let nico = {
    name: "Nico",
    greet: function() {
        console.log("Hello "+this.name);
    }
};

// Outputs: "Change the world",
nico.greet(); 

// The world 
let world = {
    name: "world"
};

// bind this of the call function to the world object
// hello world
nico.greet.call(world); 

Basic

var boundFn = function () {
  return this.x;
};
 
boundFn.call( {x: 20} ); // 20

Call(this)

var x = 20
var boundFn = function () {
  return this.x;
};
 
boundFn.call( this ); // 20

Bind and Call

Let op: Bind and Call

var boundFn = function () {
  return this.x;
}.bind( {x: 10} );
 
boundFn(); // 10
boundFn.call( {x: 20} ); // still 10

Documentation / Reference





Discover More
Javascript - Object Constructor

in Javascript of an object Invoking a function with the new operator treats it as a constructor. Unlike function calls and method calls, a constructor call passes a brand-new object as the value of this,...
Javascript Function Method
Javascript - Functions

Functions functionalities in Javascript. A function in Javascript is a variable that’s given a reference to the function being declared. The function itself is a value (as an integer or an array) that...
Chrome Dev Tool Source Debugger Scope
Javascript - This

this is a variable that has a reference value to the scope (namespace) this printThisobjthisobj Generally, this is the local scope but it may be changed. You can set this : in the call of a function...
Chrome Dev Tool Source Debugger Scope
Javascript - bind method

The bind method bind the object to the called function. Creates a new function which, when called, has its this set to the provided value, with a given sequence of arguments preceding any provided when...



Share this page:
Follow us:
Task Runner