jquery icon indicating copy to clipboard operation
jquery copied to clipboard

Should ToArray() and Each() not provide JQuery objects?

Open flimzy opened this issue 8 years ago • 4 comments

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

flimzy avatar Jul 04 '16 19:07 flimzy

you say: "iterate over elements of a jQuery object". In this example its an array of plain dom elements:

https://jsfiddle.net/m31rxf76/1/

rusco avatar Jul 06 '16 07:07 rusco

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.

flimzy avatar Jul 06 '16 08:07 flimzy

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

rusco avatar Jul 06 '16 08:07 rusco

OIC. Yes, you're right: https://jsfiddle.net/hgnxmqk8/

In that case, I suggest rather returning []*js.Object for ToArray().

flimzy avatar Jul 06 '16 10:07 flimzy