javascript - Why does slice not work directly on arguments? -


this question has answer here:

tested out on fiddle after looking @ underscore.

this seems hack call slice on arguments when not on prototype chain.

why not on prototype chain when works on arguments.

var slice = array.prototype.slice; function test () {     return slice.call(arguments,1);     // return arguments.slice(1) } var foo = test(1,2,3,4); _.each(foo, function(val){     console.log(val) }); 

>>> object.prototype.tostring.call(arguments) <<< "[object arguments]" >>> array.isarray(arguments) //is not array <<< false >>> arguments instanceof array //does not inherit array prototype either <<< false 

arguments not array object, is, not inherit array prototype. however, contains array-like structure (numeric keys , length property), array.prototype.slice can applied it. called duck typing.

oh , of course, array.prototype.slice returns array, hence can used convert array-like objects / collections new array. (ref: mdn array slice method - array-like objects)


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -