[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh/docs/Web/JavaScript/Reference/Global_Objects/Function/bind [Back]  [Original]

Function.prototype.bind() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Function.prototype.bind()

20157

Function bind() this

const module = {
  x: 42,
  getX: function () {
    return this.x;
  },
};

const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// Expected output: undefined

const boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// Expected output: 42

js
bind(thisArg)
bind(thisArg, arg1)
bind(thisArg, arg1, arg2)
bind(thisArg, arg1, arg2, /* , */ argN)

thisArg

this func null undefined new

arg1, , argN

func

this

bind() bound functiontarget function this const boundFn = fn.bind(thisArg, arg1, arg2) const boundFn = (...restArgs) => fn.call(thisArg, arg1, arg2, ...restArgs) boundFn

boundFn.bind(thisArg, /* more args */) boundFn2 thisArg boundFn2 boundFn boundFn this boundFn2 boundFn boundFn fnfn boundFn boundFn2 boundFn2

js
"use strict"; //  `this` 

function log(...args) {
  console.log(this, ...args);
}
const boundLog = log.bind("this value", 1, 2);
const boundLog2 = boundLog.bind("new this value", 3, 4);
boundLog2(5, 6); // "this value", 1, 2, 3, 4, 5, 6

new this this Reflect.construct new.target new.target

js
class Base {
  constructor(...args) {
    console.log(new.target === Base);
    console.log(args);
  }
}

const BoundBase = Base.bind(null, 1, 2);

new BoundBase(3, 4); // true, [1, 2, 3, 4]

prototype extends

js
class Derived extends class {}.bind(null) {}
// TypeError: Class extends value does not have valid prototype property undefined

instanceof instanceof prototype

js
class Base {}
const BoundBase = Base.bind(null, 1, 2);
console.log(new Base() instanceof BoundBase); // true

length

length thisArg 0

name

name "bound "

bind() this

JavaScript this

js
// thisglobalThis
this.x = 9;
const module = {
  x: 81,
  getX() {
    return this.x;
  },
};

// getXthismodule
console.log(module.getX()); // 81

const retrieveX = module.getX;
// retrieveXthisglobalThis
console.log(retrieveX()); // 9

// boundGetXthismodule
const boundGetX = retrieveX.bind(module);
console.log(boundGetX()); // 81

retrieveX this undefined globalThis retrieveX()

ECMAScript this undefined globalThis this.x = 9

Node CommonJS this module.exports globalThisretrieveX this globalThis undefinedretrieveX() undefined, this.x = 9 module.exports getX globalThis

getter Intl.NumberFormat.prototype.format()

bind()

this

js
function list(...args) {
  return args;
}

function addArguments(arg1, arg2) {
  return arg1 + arg2;
}

console.log(list(1, 2, 3)); // [1, 2, 3]

console.log(addArguments(1, 2)); // 3

// 
const leadingThirtySevenList = list.bind(null, 37);

// 
const addThirtySeven = addArguments.bind(null, 37);

console.log(leadingThirtySevenList()); // [37]
console.log(leadingThirtySevenList(1, 2, 3)); // [37, 1, 2, 3]
console.log(addThirtySeven(5)); // 42
console.log(addThirtySeven(5, 10)); // 42
// 10 

setTimeout()

setTimeout() this globalThis window this this

js
class LateBloomer {
  constructor() {
    this.petalCount = Math.floor(Math.random() * 12) + 1;
  }
  bloom() {
    //  1 
    setTimeout(this.declare.bind(this), 1000);
  }
  declare() {
    console.log(`I am a beautiful flower with ${this.petalCount} petals!`);
  }
}

const flower = new LateBloomer();
flower.bloom();
// 1 flower.declare()

js
class LateBloomer {
  bloom() {
    //  1 
    setTimeout(() => this.declare(), 1000);
  }
}

new this

js
function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function () {
  return `${this.x},${this.y}`;
};

const p = new Point(1, 2);
p.toString();
// '1,2'

// thisArg 
const YAxisPoint = Point.bind(null, 0 /*x*/);

const axisPoint = new YAxisPoint(5);
axisPoint.toString(); // '0,5'

axisPoint instanceof Point; // true
axisPoint instanceof YAxisPoint; // true
new YAxisPoint(17, 42) instanceof Point; // true

new new.targetinstanceofthis extends

new new this

js
const emptyObj = {};
const YAxisPoint = Point.bind(emptyObj, 0 /*x*/);

// 
//
YAxisPoint(13);

//  `this` 
console.log(emptyObj); // { x: 0, y: 13 }

new new new.target !== undefined class

bind()

js
class Base {
  static baseProp = "";
}

class Derived extends Base {
  static derivedProp = "";
}

const BoundDerived = Derived.bind(null);
console.log(BoundDerived.baseProp); // ""
console.log(BoundDerived.derivedProp); // undefined
console.log(new BoundDerived() instanceof Derived); // true

bind() this this array.map(callback) map(array, callback) Array.prototype arguments map

Array.prototype.slice()

js
const slice = Array.prototype.slice;

// ...

slice.call(arguments);

slice.call call() this bind() call() this slice() this Array.prototype.slice() Function.prototype.call() call()

js
// slice
const unboundSlice = Array.prototype.slice;
const slice = Function.prototype.call.bind(unboundSlice);

// ...

slice(arguments);

ECMAScript 2027 LanguageSpecification
# sec-function.prototype.bind


Web Proxy Viewer  |  New URL  |  Original Page