@CrocoDillon You are quite right and it is goo to understand why it is a more efficient way of getting the attribute value, however, if you work in a group code consistency is important and the way one codes may change slightly compared to the way one codes personally. So on work projects I’d use `$(this).attr(‘id’)` without exception.
I understand what you’re saying, and in general, I agree; especially if it is a matter of project coding standards or conventions in a team.
However, conventions and understandability don’t need to (and shouldn’t) stand in the way of efficiency. In this case, `this` is already a DOM element. Creating a new jQuery object is expensive and unnecessary. Two preferable alternatives:
… change the convention. Conventions should be about coding style, and stay away from specific logic. Yes, if you’re doing something, it’s nice for everyone to do it the same way. That doesn’t mean you should be doing anything you wouldn’t have any reason to do otherwise.
… comment your code. Then you know *for sure* everyone will understand what you’re doing.
/* same result as $( this ).attr( ‘id’ ); faster */
var el_id = this.id;
“However, conventions and understandability don’t need to (and shouldn’t) stand in the way of efficiency.”
@traq company politics may not always allow that statement to be true.
But I do completely understand and agree with what you’re saying. People should definitely develop things in the most efficient way possible, unless that micro-optimisation destroys readability and convention.