-
-
Save evilpie/1149041 to your computer and use it in GitHub Desktop.
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); | |
well (new bound) is going to return Object.create(Target.prototype)
well (new bound) is going to return Object.create(Target.prototype)
Which makes (new bound) instanceof f; // true
but not of bound
This part is explained in this gist :) (new bound) instanceof bound is equal to (new bound) instanceof f in this case.
That doesn't make (new bound) instanceof bound;
true it makes it false
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.
[[HasInstance]] of the target
will not find bound
in its [[Prototype]] look ups so it should return false
.
I think you misread the spec here it is going to check for [[Prototype]] of (new bound) being equal to target.prototype.
and that means bound
is right out, it should return false
.
Sleep over it and come back here tomorrow. I am going to enjoy the sun :)
Well crap, you guys are right. Here is the V8 bug report and explanation by Luke that made it click for me :D
(new bound) instanceof bound; // should be false
The target does not contain the
new bounds
[[Prototype]] as it's ancestors.bound
and thetarget
are totally disconnected.