Closed Bug 1412120 Opened 8 years ago Closed 8 years ago

Array.prototype.join() does not work with "array-like objects" (at least not in all environments)

Categories

(Developer Documentation Graveyard :: JavaScript, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: darren_adkinson, Unassigned)

Details

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 Steps to reproduce: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join The first line: "The join() method joins all elements of an array (or an array-like object) into a string and returns this string." appears to be incorrect, or at least potentially misleading, as far as certain environments are concerned. It seems necessary to convert arguments to a true array before calling join(), from what I can tell.
https://tc39.github.io/ecma262/#sec-array.prototype.join per spec, it should work on array-like object. what's "certain environments" ?
Flags: needinfo?(darren_adkinson)
arguments is an "array-like object" but, afaik, you have to turn it into a true array in order to call 'join' on it. From what I've seen the other array methods don't mention 'or an array-like object' so this seemed to imply there was something about 'join' you could do that you can't with, say, slice. ``` var fn = function (a, b, c) { var argList = arguments.join(); console.log(argList); } undefined fn(1, 2, 3); Uncaught TypeError: arguments.join is not a function at fn (<anonymous>:2:27) at <anonymous>:1:1 fn @ VM154:2 (anonymous) @ VM162:1 var fn = function (a, b, c) { var argList = [].slice.call(arguments).join(); console.log(argList); } undefined fn(1, 2, 3); 1,2,3 undefined ```
Flags: needinfo?(darren_adkinson)
the implementation of Array.prototype.join works on array-like objects. then, join is the property of Array.prototype, so you cannot get join property from arguments or other array-like object, unless it has the property. so, you need to use Function.prototype.call [1] or Function.prototype.apply [2] on Array.prototype.join, like: var obj = { 0: "a", 1: "b", 2: "c", length: 3 }; console.log(Array.prototype.join.call(obj)); or set join property explicitly, like: var obj = { 0: "a", 1: "b", 2: "c", length: 3, join: Array.prototype.join }; console.log(obj.join()); [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.