bind

  • Revision slug: Talk:JavaScript/Reference/Global_Objects/Function/bind
  • Revision title: bind
  • Revision id: 79966
  • Created:
  • Creator: silentmatt
  • Is current revision? No
  • Comment Added section about the change to bound.prototype; 198 words added

Revision Content

(function(){return this;}).bind(null)() === window; //this returns true in native implementations and false in the current compatibility code.

On line 11, "( obj || {} )" if this read "( obj || (window || {}) )" it would behave like the native bind when "thisArg" is null.

Another issue on line 11 in Safari (I was running SF5 for windows) meant I needed to change this:

"this instanceof nop" to this: "(nop.prototype && this instanceof nop)"

To test this in safari, execute this line (after adding the bind compatibility code of course):

console.log.bind(console, "foo")("bar")

nop function is useless

Line 17 of the partial implementation was recently changed to "bound.prototype = this.prototype;". It used to be two lines:

nop.prototype = self.prototype;
bound.prototype = new nop();

Which made the check for "this instanceof nop" work when "bound" is called as a constructor. As it is, the check will always fail since it's impossible to create an object that gets nop's prototype. The change appears to be attempting to keep the existing prototype property, but from looking at the spec, the bound function isn't supposed to have a .prototype anyway. I haven't dug into enough to know for sure if the old code was correct so I'm not changing it back, but as it is now, some of the code is useless and the returned function doesn't work correctly as a constructor.

 

<meta http-equiv="content-type" content="text/html; charset=utf-8"/>

Revision Source

<p>(function(){return this;}).bind(null)() === window; //this returns true in native implementations and false in the current compatibility code.</p>
<p>On line 11, "( obj || {} )" if this read "( obj || (window || {}) )" it would behave like the native bind when "thisArg" is null.</p>
<p>Another issue on line 11 in Safari (I was running SF5 for windows) meant I needed to change this:</p>
<p>"this instanceof nop" to this: "(nop.prototype &amp;&amp; this instanceof nop)"</p>
<p>To test this in safari, execute this line (after adding the bind compatibility code of course):</p>
<p>console.log.bind(console, "foo")("bar")</p>
<h3>nop function is useless</h3>
<p>Line 17 of the partial implementation was recently changed to "bound.prototype = this.prototype;". It used to be two lines:</p>
<p style="margin-left: 40px; ">nop.prototype = self.prototype;<br>
bound.prototype = new nop();</p>
<p>Which made the check for "this instanceof nop" work when "bound" is called as a constructor. As it is, the check will always fail since it's impossible to create an object that gets nop's prototype. The change appears to be attempting to keep the existing prototype property, but from looking at the spec, the bound function isn't supposed to have a .prototype anyway. I haven't dug into enough to know for sure if the old code was correct so I'm not changing it back, but as it is now, some of the code is useless and the returned function doesn't work correctly as a constructor.</p>
<p>
 </p>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"/&gt;
Revert to this revision