Last active
August 3, 2016 01:20
-
-
Save jnvm/1aa815fd006283166a1f9a40d68a839c to your computer and use it in GitHub Desktop.
in which jnvm makes a fool of himself claiming: bind modifies function, doesn't return new one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var echo=function(){ | |
var echoes=echo.count||10 | |
while(echoes--) this.makeNoise() | |
} | |
var dog={ | |
sound:"woof", | |
makeNoise:function(){ console.log(this.sound) }, | |
} | |
echo.count=3 | |
var barkAtGrandCanyon=echo.bind(dog) | |
barkAtGrandCanyon()//echoes 3x, since it's the same function. If it were a new function, it wouldn't have the count property. | |
//(^FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE^) | |
echo.count=20 | |
barkAtGrandCanyon()//echoes 20x |
..agreed! I didn't actually construct a test of my claim! Bah. Thank you & very sorry.
It's even the first sentence in mdn:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
The bind() method creates a new function
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bind absolutely creates a new function.
It echoes 3x times because it references the count property on the old function on line 2.
Try adding these lines at the end, and you'll see: