Skip to content

toString should output '[object FormData]'#346

Merged
alexindigo merged 1 commit intoform-data:masterfrom
m59peacemaker:master
Apr 9, 2017
Merged

toString should output '[object FormData]'#346
alexindigo merged 1 commit intoform-data:masterfrom
m59peacemaker:master

Conversation

@m59peacemaker
Copy link
Copy Markdown
Contributor

new FormData().toString() currently outputs '[object Object]', but it should output '[object FormData]'.

@alexindigo
Copy link
Copy Markdown
Member

Hey, thanks for the PR. Do you mind to elaborate on the use case?

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 7, 2017

Coverage Status

Coverage increased (+0.02%) to 97.949% when pulling 67b8a8e on m59peacemaker:master into 03444d2 on form-data:master.

@m59peacemaker
Copy link
Copy Markdown
Contributor Author

I'm not sure about a specification about this, but every object in JavaScript that is a native type has its own matching toString() result.

new Map().toString() // => '[object Map]'
[].toString() // => '[object Array]'
new Request({}) // =>'[object Request]'

and so on. Any environment where FormData is natively available, new FormData().toString() will behave in accordance with my PR. I noticed it in a case where I wanted to see if an object was an instance of FormData without having to require the actual object. instanceof has a history of being unreliable anyway, and I'm not sure if those issues have been resolved.

@m59peacemaker
Copy link
Copy Markdown
Contributor Author

m59peacemaker commented Apr 8, 2017

I found the spec. http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf Section: 15.2.4.2

15.2.4.2 Object.prototype.toString()
When the toString method is called, the following steps are taken:

  1. Get the [[Class]] property of this object.
  2. Call ToString(Result(1)).
  3. Compute a string value by concatenating the three strings "[object ", Result(2), and "]".
  4. Return Result(3).

@alexindigo
Copy link
Copy Markdown
Member

I see. To make it more like the real thing, makes sense. Thanks a lot.
Merging and after all CI stuff is done will publish patch version.

@alexindigo alexindigo merged commit 1974877 into form-data:master Apr 9, 2017
@m59peacemaker
Copy link
Copy Markdown
Contributor Author

Fantastic. Thanks! It's a good thing for any object that acts as a special type in JavaScript, even if it isn't native such as this case. For example, I was just logging some Immutable.js types and they have their own toString(). https://github.com/facebook/immutable-js/blob/c93bb75ef238371bf3003337c905b1eb31bc2045/src/List.js#L67-L69

@alexindigo
Copy link
Copy Markdown
Member

Yeah, I prefer that kind of more meaningful toStrings, maybe something like urlencoded parameters, although it'd be hard to support all kinds of values. And we need to follow native thing anyway.

@alexindigo
Copy link
Copy Markdown
Member

Published 2.1.4. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants