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

Function.prototype.bind() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Function.prototype.bind()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

bind() , this . , .

In this article

fun.bind(thisArg[, arg1[, arg2[, ...]]])

thisArg

, this . , new.

arg1, arg2, ...

, .

bind() " " (). - " " ( ECMAScript 6 ), . .

( ) :

  • [[BoundTargetFunction]] - ( )
  • [[BoundThis]] - , this .
  • [[BoundArguments]] - , .
  • [[Call]] - . ( ), .

, [[Call]] Call(target, boundThis, args).

  • target - [[BoundTargetFunction]];
  • boundThis - [[BoundThis]];
  • args - [[BoundArguments]].

new: , . this , .

:

bind() , , , this. JavaScript , this (, -). , , . , , :

js
this.x = 9;
var module = {
  x: 81,
  getX: function () {
    return this.x;
  },
};

module.getX(); // 81

var getX = module.getX;
getX(); // 9,     this    

//     this,   module
var boundGetX = getX.bind(module);
boundGetX(); // 81

:

bind() . ( ) this , .

js
function list() {
  return Array.prototype.slice.call(arguments);
}

var list1 = list(1, 2, 3); // [1, 2, 3]

//      
var leadingThirtysevenList = list.bind(undefined, 37);

var list2 = leadingThirtysevenList(); // [37]
var list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]

: setTimeout

, window.setTimeout() this window ( global). , this , this - .

js
function LateBloomer() {
  this.petalCount = Math.ceil(Math.random() * 12) + 1;
}

//      1 
LateBloomer.prototype.bloom = function () {
  window.setTimeout(this.declare.bind(this), 1000);
};

LateBloomer.prototype.declare = function () {
  console.log("    " + this.petalCount + " !");
};

: ,

: JavaScript bind(). , , .

new , . , this . , :

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

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

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

var emptyObj = {};
var YAxisPoint = Point.bind(emptyObj, 0 /*x*/);
//   ,  ,
//      bind:
var YAxisPoint = Point.bind(null, 0 /*x*/);

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

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

, , new. , , , , new.

js
//         JavaScript
// ...  

//       
// (    )
YAxisPoint(13);

emptyObj.x + "," + emptyObj.y;
// >  '0,13'

new, , .

:

bind() , , this.

, , Array.prototype.slice, . :

js
var slice = Array.prototype.slice;

// ...

slice.call(arguments);

bind(), . slice , call() Function, this, slice() Array. , call() :

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

// ...

slice(arguments);

bind ECMA-262 5- ; . , , bind() , .

js
if (!Function.prototype.bind) {
  Function.prototype.bind = function (oThis) {
    if (typeof this !== "function") {
      //    
      // IsCallable  ECMAScript 5
      throw new TypeError(
        "Function.prototype.bind - what is trying to be bound is not callable",
      );
    }

    var aArgs = Array.prototype.slice.call(arguments, 1),
      fToBind = this,
      fNOP = function () {},
      fBound = function () {
        return fToBind.apply(
          this instanceof fNOP && oThis ? this : oThis,
          aArgs.concat(Array.prototype.slice.call(arguments)),
        );
      };

    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();

    return fBound;
  };
}

( , ) :

, , ECMA-262 5- ! , (, , ), , bind() .

Specification
ECMAScript 2027 LanguageSpecification
# sec-function.prototype.bind


Web Proxy Viewer  |  New URL  |  Original Page