Javascript - bind method

Chrome Dev Tool Source Debugger Scope

About

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 the new function was called.

In non-strict mode, the this of class methods or function is bound by default to their outer scope. In strict mode, the this of class methods or function is bound by default to the inner scope.

Example

Bind

var x = "globalValue";
var fn = function () {
  return this.x;
};

var fnBound = fn.bind( {x: "bindValue"} );
 
console.log( 'fn: ' + fn() ); // GlobalValue
console.log( 'fnBound: ' + fnBound() ); // bindValue

Bind and Call

Bind and Call

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

Documentation / Reference





Discover More
Chrome Dev Tool Source Debugger Scope
Javascript - Call method

Calls (executes) a function and sets its this to the provided value binded , arguments can be passed as they are. call(this)context (scope) Bind ECMA-262-3...
Javascript - Class (ES6)

This article is Class in Javascript. Class was introduced in ECMAScript 2015. Before the module pattern was used to create a class-like behavior. Classes provide a simpler way to create objects and...
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...



Share this page:
Follow us:
Task Runner