bind()  Function  instance method JS Home  <<  JS Reference  <<  Function  <<  bind()

Description

Creates a new function that when called sets its this keyword to the provided value, with a given sequence of arguments that precede any provided on function call.

Syntax

Signature Description
aFunction.bind(thisArg[, arg1[, arg2[, ...argN]]])Creates a new function with a given this value and arguments provided individually.

Parameters

Parameter Description
thisArgThe value of this provided for the call to the function.
arg1,arg2,...argNThe arguments with which the function should be called, null or undefined if no arguments required.

Examples

Below is an example of using the bind() method.


const module = {
  str: ": A String was returned",
  getString: function() {
    return this.str;
  }
};

// The function gets invoked at the global scope
const unboundGetString = module.getString;
alert(unboundGetString() + ": expected output: undefined");

const boundGetString = unboundGetString.bind(module);
alert(boundGetString() + ": expected output: A String was returned"); 

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 8 - Functions

JavaScript Basics

JavaScript Basics

JavaScript Intermediate

JavaScript Intermediate

JavaScript Advanced

JavaScript Advanced

JavaScript Reference

JavaScript Entities

Globals

Function

Class Properties
prototype
Instance Properties
constructor
length
Class Methods
None.
Instance Methods Getters
apply()
bind()
call()
toString()
Instance Methods Setters
None.

Statements

Operators