JS: Function.prototype.bind

By Xah Lee. Date: . Last updated: .

[see JS: “this” Binding]

// example of using Function.prototype.bind

// object with 1 property ff.
const aa = { "ff":function () { return this;} };

// new object bb
const bb = {"b":2};

// create a new function, that is ff with thisBinding of bb
const gg = aa.ff.bind(bb);

console.log( gg() ); // { b: 2}

Example of using Function.prototype.bind with default argument:

// Example of using Function.prototype.bind with default argument

function ff(x) { this["p"] = x; };

const bb = {"b":2};

// create a new function
// with default ThisBinding and a default arg
const gg = ff.bind(bb,7);

// call gg
gg();
// note, no args are given.

console.log( bb ); // { b: 2, p: 7 }

Reference

ECMAScript 2015 §Fundamental Objects#sec-function.prototype.bind

Function Topic

  1. JS: Define Function
  2. JS: Functional Programing
  3. JS: Arrow Function
  4. JS: Function Parameters
  5. JS: f Declaration vs Expression
  6. JS: Closure
  7. JS: Function Call, Apply, Bind
  8. JS: Function Argument Default Value
  9. JS: Function Rest Parameters
  10. JS: Function Argument Destructure
  11. JS: Function Object
  12. JS: Function.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc