Trying to see how method is called ie. if $(‘.test’) or $(‘.test’).hide() , if first one return element if second return object

I am trying to recreate the $() selector and so far all is well. One thing I am hung up on is how to return an element rather than the object if no method is being called.

For example:

I have a function

function $(selector){

return new DomEl(selector); //this would be the simialr ot the jquer object that has all methods

}

The above allows me to call a methods on the element like so:

$('#test').hide();

But if I just use the function without calling any class like so: $('#test')

I want it to return the element:

return document.querySelector(selector)

So my question is how can I detect how the $() function is being called and whether or not it is being called with a class method to be called upon it ?

Thanks for your help!



Read more here: https://stackoverflow.com/questions/65711492/trying-to-see-how-method-is-called-ie-if-test-or-test-hide-if-f

Content Attribution

This content was originally published by Qwerty at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: