Last active
May 28, 2018 09:35
-
-
Save colingourlay/8190585 to your computer and use it in GitHub Desktop.
Function.prototype.apply 's handling of an object instead of an array. It forces the object through Array.prototype.slice function as 'this' to create an array, pretty much what we do to turn an arguments object into a real array.
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
Array.apply(null, [1, 2, 3]) | |
> [1, 2, 3] | |
Array.apply(null, [1, 2, 3]).length | |
> 3 | |
Array.apply(null, {length:3}) | |
> [undefined, undefined, undefined] | |
Array.apply(null, {length:3}).length | |
> 3 | |
Array.apply(null, Array.prototype.slice.call({length:3})) | |
> [undefined, undefined, undefined] | |
Array.apply(null, Array.protorype.slice.call({length:3})).length | |
> 3 |
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
Function.apply(null, ['a', 'b', 'return a + b']) | |
> function anonymous(a,b | |
/**/) { | |
return a + b | |
} | |
Function.apply(null, ['a', 'b', 'return a + b']).length | |
> 2 | |
Function.apply(null, {length:3}) | |
> function anonymous(undefined,undefined | |
/**/) { | |
undefined | |
} | |
Function.apply(null, {length:3}).length | |
> 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment