| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/bind | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
bind() , this . , .
fun.bind(thisArg[, arg1[, arg2[, ...]]])
thisArg, this . , new.
arg1, arg2, ..., .
bind() " " (). - " " ( ECMAScript 6 ), . .
( ) :
, [[Call]] Call(target, boundThis, args).
new: , . this , .
bind() , , , this. JavaScript , this (, -). , , . , , :
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 , .
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 - .
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 . , :
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
// JavaScript
// ...
//
// ( )
YAxisPoint(13);
emptyObj.x + "," + emptyObj.y;
// > '0,13'
new, , .
bind() , , this.
, , Array.prototype.slice, . :
var slice = Array.prototype.slice;
// ...
slice.call(arguments);
bind(), . slice , call() Function, this, slice() Array. , call() :
// , slice
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.call.bind(unboundSlice);
// ...
slice(arguments);
bind ECMA-262 5- ; . , , bind() , .
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;
};
}
( , ) :
Array.prototype.slice(), Array.prototype.concat(), Function.prototype.call() Function.prototype.apply() , .caller arguments TypeError , . ( , Object.defineProperty, [ ---], Object.prototype.__defineGetter__() Object.prototype.__defineSetter__().)prototype. ( .)length ECMA-262; 0, , , length , , . , , ECMA-262 5- ! , (, , ), , bind() .
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-function.prototype.bind |
This page was last modified on 29 . 2026 . by MDN contributors.
FunctionObject/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |