1

https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-function.prototype.bind:

I can't understand the notes:

Note 1

Function objects created using Function.prototype.bind are exotic objects. They also do not have a "prototype" property.

Note 2

If Target is either an arrow function or a bound function exotic object, then the thisArg passed to this method will not be used by subsequent calls to F.

Can someone give some examples?

2 Answers 2

3

Regarding Note 1:

const bound = (function(){}).bind();
console.log(bound instanceof Function);
console.log(!("prototype" in bound));
// being an "exotic object" just means that it behaves unusual - in this case,
// having a special [[call]] operation

Regarding Note 2:

function example() { "use strict"; console.log(this); }
const bound = example.bind("Hello");
const boundAgain = bound.bind("World");
console.log(bound(), boundAgain()); // Hello Hello

function makeArrow() { "use strict"; return () => console.log(this); }
const arrow = makeArrow.call("Hello");
const boundArrow = bound.bind("World");
console.log(arrow(), boundArrow()); // Hello Hello
Sign up to request clarification or add additional context in comments.

Comments

1

Note 1: Bound functions do not have prototypes

const target = { foo: "bar" };
const f = function() {};
const bound = f.bind(target);
console.log("f.prototype", f.prototype);
console.log("bound.prototype", bound.prototype);

VM439:4 f.prototype {constructor: ƒ}
VM439:5 bound.prototype undefined

Note 2: Arrow functions cannot be bound

const target = { foo: "bar", toString: function() { return "foobar"; } };
const normal = function() { return this; };
const arrow = () => { return this; };
const boundNormal = normal.bind(target);
const boundArrow = arrow.bind(target);
console.log("normal()", normal());
console.log("boundNormal()", boundNormal());
console.log("arrow()", arrow());
console.log("boundArrow()", boundArrow());
console.log("normal.call(target)", normal.call(target));
console.log("arrow.call(target)", arrow.call(target));

normal() Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}
boundNormal() {foo: "bar", toString: ƒ}
arrow() Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}
boundArrow() Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}
normal.call(target) {foo: "bar", toString: ƒ}
arrow.call(target) Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}

2 Comments

Arrow function can be bound (creatiing the boundArrow in your example), but they of course ignore the bound this argument as they still use lexical scoping.
Yes, I suppose it depends what you mean by "bound." If it doesn't behave any differently, I wouldn't consider it "bound." But yes, you can call bind() on it (as seen in my example)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.