Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Function Borrowing

Function Borrowing

The bind() method allows an object to borrow a method from another object.

In this example, two objects (person and member) are created, and the member object borrows the fullname method from the person object.

Example

const person = {
  firstName:“John”,
  lastName: “Doe”,
  fullName: function () {
    return this.firstName + ” “ + this.lastName;
  }
}

const member = {
  firstName:“Hege”,
  lastName: “Nilsen”,
}

let fullName = person.fullName.bind(member);

This Precedence

To determine which object this refers to, follow this order of precedence.

Precedence

Object

1

bind()

2

apply() and call()

3

Object method

4

Global scope

  • Is this in a function called using bind()?
  • Is this in a function called using apply()?
  • Is this in a function called using call()?
  • Is this in an object method (function)?
  • Is this in a function in the global scope?