0

Like how the jQuery uses the jQuery method for return a jQuery object, but how a format of collection (Array, NodeList,...). I wonder how jQuery does.

That way I can convert a NodeList object, for example, on a personal object.

jQuery("li") //=> [<li>...</li>, <li>...</li>, <li>...</li>]
jQuery("li") instanteof jQuery //=> true
jQuery("li") instanteof Array //=> false
jQuery("li") instanteof NodeList //=> false
jQuery("li") instanteof HTMLCollection //=> false
rplaurindo
  • 1,277
  • 14
  • 23
  • `jQuery("body").toArray()` or `.get()` does what you want. – Bergi Nov 26 '15 at 02:12
  • There is no (straightforward) way to create your own `NodeList` or `HTMLCollection`. But there's absolutely zero reason to do so either. What would you want this for? – Bergi Nov 26 '15 at 02:13
  • @Bergi For Knowledge. I think you got it wrong. I do not want to convert a jQuery object to an array. I want to know how jQuery to create a collection that is not native of the JS. – rplaurindo Nov 26 '15 at 02:28
  • I'm not sure what you mean by "pseudo collection" or "create a collection that is not native". jQuery just creates instances like every other class as well; `function jQuery(){}; var x = new jQuery; x instanceof jQuery`. No magic here. – Bergi Nov 26 '15 at 02:34
  • @Bergi Yes, you're right, but we need not explicitly instantiate the jQuery. I find it cool. Just make a call to jQuery class and pass a parameter to it, then it returns in an instance of itself, but as a collection of objects. And this collection is not instance of array. – rplaurindo Nov 26 '15 at 02:39
  • 1
    Returning an instance even if called without `new` [is pretty simple](http://stackoverflow.com/q/20859985/1048572). That jQuery instances have integer properties and a `.length` like arrays isn't anything special either. – Bergi Nov 26 '15 at 02:49
  • @Bergi Perfect. Thank so much!!! Example of the compilation [link text]http://jsfiddle.net/rplaurindo/fheg7bs0/1/"[/link] – rplaurindo Nov 26 '15 at 03:35
  • 1
    Uh, did I mention that [you should not use this pattern](http://stackoverflow.com/a/12143833/1048572)? Don't make `build` the actual constructor, it's confusing as hell. – Bergi Nov 26 '15 at 03:39
  • @bergi how to inserting as text link here? – rplaurindo Nov 26 '15 at 03:42
  • 1
    [Just regular markdown](http://stackoverflow.com/editing-help#comment-formatting) :-) – Bergi Nov 26 '15 at 03:46
  • @Bergi thanks. What better way of dealing with the builder? Instantiate explicitly? – rplaurindo Nov 26 '15 at 03:47
  • 1
    Either put the `if (!(this instanceof My…)) return new My…(…);` right in the regular constructor, or - if you feel having to use a factory and a separate constructor - don't put the actual constructor on the `.prototype` of the factory function. – Bergi Nov 26 '15 at 03:50
  • @Bergi like [this](http://jsfiddle.net/rplaurindo/fheg7bs0/3/)? – rplaurindo Nov 26 '15 at 03:55
  • 1
    [Yes](http://jsfiddle.net/fheg7bs0/4/) – Bergi Nov 26 '15 at 04:06

0 Answers0