| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/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 7.
bind() . value 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
func.bind(thisArg[, arg1[, arg2[, ...]]])
thisArg (target function) this . new . bind setTimeout , thisArg . bind (argument) this thisArg .
arg1, arg2, ....
this .
bind() . , ECMAScript 2015 (exotic function object). .
.
[[BoundTargetFunction]] - (wrapped) .[[BoundThis]] - .[[BoundArguments]] - .[[Call]] - . . this . [[BoundTargetFunction]] [[Call]] . [[Call]] Call(boundThis, args) . , boundThis [[BoundThis]], args [[BoundArguments]] .
new : . this , (prepend) .
bind() this . JavaScript , this ( : ). , . , .
this.x = 9;
var module = {
x: 81,
getX: function () {
return this.x;
},
};
module.getX(); // 81
var retrieveX = module.getX;
retrieveX();
// 9 -
// module 'this'
// x
// module x
var boundGetX = retrieveX.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(null, 37);
var list2 = leadingThirtysevenList(); // [37]
var list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]
function addArguments(arg1, arg2) {
return arg1 + arg2;
}
var result1 = addArguments(1, 2); // 3
// .
var addThirtySeven = addArguments.bind(null, 37);
var result2 = addThirtySeven(5); // 37 + 5 = 42
// .
var result3 = addThirtySeven(5, 10); // 37 + 5 = 42
setTimeout window.setTimeout() , this window ( global) . this , this , .
function LateBloomer() {
this.petalCount = Math.ceil(Math.random() * 12) + 1;
}
// 1 bloom
LateBloomer.prototype.bloom = function () {
window.setTimeout(this.declare.bind(this), 1000);
};
LateBloomer.prototype.declare = function () {
console.log("I am a beautiful flower with " + this.petalCount + " petals!");
};
var flower = new LateBloomer();
flower.bloom();
// 1 , 'declare'
:
JavaScript bind() (edge case) . .
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'
// ,
// bind :
var YAxisPoint = Point.bind(null, 0 /*x*/);
var emptyObj = {};
var YAxisPoint = Point.bind(emptyObj, 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 (shortcut) .
, Array.prototype.slice . :
var slice = Array.prototype.slice;
// ...
slice.apply(arguments);
bind(), . , slice Function apply() , this Array slice() . apply() :
// "slice"
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.apply.bind(unboundSlice);
// ...
slice(arguments);
bind ECMA-262 5 ; . , bind() .
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// ECMAScript 5 IsCallable
//
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 ? this : oThis,
aArgs.concat(Array.prototype.slice.call(arguments)),
);
};
if (this.prototype) {
// Function.prototype prototype
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}
( , ) :
Array.prototype.slice(), Array.prototype.concat(), Function.prototype.call() Function.prototype.apply(), .caller get, set TypeError arguments . ( Object.defineProperty Object.prototype.__defineGetter__() Object.prototype.__defineSetter__() [ (throw-on-delete) (behavior) ] .)prototype . ( .)length ECMA-262 (mandated) : 0 , 0 . , ECMA-262 5 ! ( ), bind() .
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-function.prototype.bind |
This page was last modified on 2026 7 15 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 |