jquery
jquery copied to clipboard
Should ToArray() and Each() not provide JQuery objects?
There may be other such instances, but at least the two functions ToArray()
and Each()
iterate over elements of a jQuery object, exposing each element as another jQuery object in turn. However, the current implementation exposes them as interface{}
s, which must then be coerced back into jquery.JQuery
objects. Wouldn't it be more natural to just expose these directly? Would this ever be incorrect? Specifically, change the functions to have these signatures:
func (j JQuery) ToArray() []JQuery
func (j JQuery) Each(fn func(int, JQuery)) JQuery
you say: "iterate over elements of a jQuery object". In this example its an array of plain dom elements:
https://jsfiddle.net/m31rxf76/1/
I'm not exactly sure what distinction you're making, @rusco. Probably I spoke unclearly.
In straight jQuery, toArray()
returns an array of jQuery objects. I'm suggesting we honor that behavior here, and return []JQuery
rather than returning []interface{}
. Beneath, both are the same--both represent JQuery objects. It's just a matter of not having to convert []interface{}
back to []JQuery
manually.
I am just questioning the assumptions you make: "... returns an array of jQuery objects". I think it's an array of plain (untyped) objects.
The typescript definition files say it's "any[]" in case of toArray and an "element" as an argument for each:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/jquery/jquery.d.ts#L2784
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/jquery/jquery.d.ts#L2830
OIC. Yes, you're right: https://jsfiddle.net/hgnxmqk8/
In that case, I suggest rather returning []*js.Object
for ToArray()
.