Quantcast
Channel: JQuery UI » Tips and Tricks
Viewing all articles
Browse latest Browse all 13

jQuery overturn selector for better readability

$
0
0

What do I mean by this?

Well it’s a really simple idea:

  • I like chaining
  • I like jQuery
  • I DON’T really like using the method I think it makes the code a little unreadable.
  • I do like my code to be a little readable.

So here is the simple solution:

(function () { 
Object.defineProperty(String.prototype, "$", {
   get: function (){
     var selector = this.toString();
     return $(selector);
   }
})
}())

What does this mean (for some of us)?

Well this simply means that we can do the following. (please note: both are equivalent).

// select the body element
$('body');
"body".$; 

//Find element
$('body').find('div');
"body".$.find('div');

//Single Quotes are allowed too Since it's a string
'body'.$.find('div').show()

WHY?

I thought it would be nice to have the selector first and actions, or manipulations after

I would love to see this in my code…

'body'              //selector
     .$             //getter
     .find('div')   //find all divs
     .show()        //change style="display:block;"
;//end of chain

As per usual comments and thoughts are welcomed.


Viewing all articles
Browse latest Browse all 13

Trending Articles