Bugzilla – Bug 13681
Make FormData.append return the FormData object
Last modified: 2012-11-22 21:58:14 UTC
If we change the FormData.append function to return the FormData object on which the function was called, it would allow syntax like
myformdata.append("foo", file).append("bar", "text").append("baz", blob);
as well as
xhr.send((new FormData).append("afile", myfile));
We do not use that pattern anywhere else as far as I know.
That's a terrible reason not to do something! There are lots of things that the DOM didn't used to do but which were a good idea and that we're slowly starting to do now.
Do you have a technical reason why you don't think we should provide the nicer syntax in comment 0?
Is it established chaining is a good idea? What other APIs use this?
There are lots of DOM methods that could adopt this syntax. It's one of the things that helps make jQuery concise: you can chain repeated method calls without having to repeat the thing you're calling it on. In many cases you can completely avoid temporaries. Like consider
var element = document.createElement("foo");
which is one extra line with an extra temporary. Is there any reason we shouldn't adopt this convention across the board for DOM methods with void return type? Compat risk is conceivable, but I shouldn't think it's at all certain, and it seems worth it to try.
I've added an agenda item for the coming TC39 meeting to discuss |this| returns in new API. See also: http://lists.w3.org/Archives/Public/public-script-coord/2012OctDec/0164.html