[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/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 7.

bind() . value this , .

In this article

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
    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 ( : ). , . , .

js
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 , .

js
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 , .

js
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 . , () :

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'

//    ,

//  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

new . , new .

js
//  JavaScript     
// ... 

//      
// (   )
YAxisPoint(13);

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

new , .

bind() this (shortcut) .

, Array.prototype.slice . :

js
var slice = Array.prototype.slice;

// ...

slice.apply(arguments);

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

js
//   "slice" 
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.apply.bind(unboundSlice);

// ...

slice(arguments);

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

js
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;
  };
}

( , ) :

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

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


Web Proxy Viewer  |  New URL  |  Original Page