Last active
December 17, 2015 18:59
-
-
Save roden0/5657121 to your computer and use it in GitHub Desktop.
Jquey Best practices.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bad | |
| $("a").click(function(e) { | |
| $(this).addClass("active"); | |
| $(this).parent().siblings().find("a").removeClass("active"); | |
| var o = $(this).offset(); | |
| }); | |
| // Better | |
| $("a").click(function(e) { | |
| var $t = $(this), | |
| o = $t.offset(); | |
| $t.addClass("active"); | |
| $t.parent().siblings().find("a").removeClass("active"); | |
| }); | |
| $("a").click(function(e) { | |
| // Bad | |
| var id = $(this).attr("id"); | |
| // Good | |
| var id = this.id; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var catApp = { | |
| $cats: $(".cat"), | |
| init: function() { | |
| this.$cats.click( catClicked ) | |
| }, | |
| catClicked: function(e) { | |
| alert("'Meow', said " + $(e.target).data("name") ); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment