es-shims / es5-shim

ECMAScript 5 compatibility shims for legacy (and modern) JavaScript engines
MIT License
7.12k stars 895 forks source link

Why not set bound.prototype = target.prototype directly? #463

Open LongTengDao opened 5 years ago

LongTengDao commented 5 years ago

https://github.com/es-shims/es5-shim/blob/d5297adf9a9ca5669b3d43153fa2d8092e879f9c/es5-shim.js#L307

function f () {}
var b = f.bind({});
new f instanceof b;// true in native, but false in shim
ljharb commented 5 years ago

When I overwrite Function.prototype.bind in my current browser with the shim's implementation, new f instanceof b returns true for me. In what browser are you seeing it return false?

ljharb commented 5 years ago

(Note that it would be incorrect to set it directly; b.prototype !== f.prototype in native)

LongTengDao commented 5 years ago
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="./es5-shim.js"></script>
    </head>
    <body>
        <script>
            function F () {}
            var B = F.bind([]);
            var f = new F;
            document.write(f instanceof B);
        </script>
    </body>
</html>

IE 6 7 8 all print false.

In current shim, B.prototype is instance of F, in other words, new F and B.prototype are same level instance.

(! ( B.prototype instanceof F ) in native too)