Skip to content

Instantly share code, notes, and snippets.

@colingourlay
Last active May 28, 2018 09:35
Show Gist options
  • Save colingourlay/8190585 to your computer and use it in GitHub Desktop.
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.
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
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