Skip to content

Instantly share code, notes, and snippets.

@evilpie
Created August 16, 2011 13:12
Show Gist options
  • Save evilpie/1149041 to your computer and use it in GitHub Desktop.
Save evilpie/1149041 to your computer and use it in GitHub Desktop.
blabla
so well
var f = function () {};
var bound = f.bind({});
(new bound) instanceof bound;
V = new bound;
bound[[HasInstance]](V);
/* inside 15.3.4.5.3 [[HasInstance]] (V) */
if (!bound[[Target]))
throw;
target = bound[[Target]]
target[[HasInstance]](V)
/* target is just |f| */
/* at this point this is the same as (new bound) instanceof f */
/* 15.3.5.3 [[HasInstance]] (V) */
if (!IsObject(V))
throw;
prototype = target.prototype;
do {
V = V.__proto__; /* V proto is going to be f.prototype, because (new bound) is about the same as (new f)
if (V === null)
return false;
if (V === prototype)
return true; /* yaay ! */
} while (true);
@jdalton
Copy link

jdalton commented Aug 16, 2011

(new bound) instanceof bound; // should be false
The target does not contain the new bounds [[Prototype]] as it's ancestors.
bound and the target are totally disconnected.

@evilpie
Copy link
Author

evilpie commented Aug 16, 2011

well (new bound) is going to return Object.create(Target.prototype)

@jdalton
Copy link

jdalton commented Aug 16, 2011

well (new bound) is going to return Object.create(Target.prototype)

Which makes (new bound) instanceof f; // true but not of bound

@evilpie
Copy link
Author

evilpie commented Aug 16, 2011

This part is explained in this gist :) (new bound) instanceof bound is equal to (new bound) instanceof f in this case.

@jdalton
Copy link

jdalton commented Aug 16, 2011

That doesn't make (new bound) instanceof bound; true it makes it false

@DavidBruant
Copy link

When new bound is called, bound.[[Construct]] is called bound.[[Construct]] is f.[[Construct]] per ES5.1 15.3.4.5 step 13), so |new bound| is like doing |new f|, setting the correct [[prototype]] to the object to be built (f.prototype).
This value is used for testing when bound.[[HasInstance]](which has the return value of f.[[HasInstance]]) is called.

bound.[[Construct]] being f.[[Construct]] and bound.[[HasInstance]] being f.[[HasInstance]] make the two objects enough "connected" so that "new bound instanceof bound" is equivalent to "new f instanceof f" (which is true). If you still disagree, let's post on es5-discuss, hopefully the people who wrote the spec will be able to tell us 1) their intention 2) if the spec matches it.

@jdalton
Copy link

jdalton commented Aug 16, 2011

[[HasInstance]] of the target will not find bound in its [[Prototype]] look ups so it should return false.

@evilpie
Copy link
Author

evilpie commented Aug 16, 2011

I think you misread the spec here it is going to check for [[Prototype]] of (new bound) being equal to target.prototype.

@jdalton
Copy link

jdalton commented Aug 16, 2011

and that means bound is right out, it should return false.

@evilpie
Copy link
Author

evilpie commented Aug 16, 2011

Sleep over it and come back here tomorrow. I am going to enjoy the sun :)

@jdalton
Copy link

jdalton commented Aug 16, 2011

Well crap, you guys are right. Here is the V8 bug report and explanation by Luke that made it click for me :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment