-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJquery.js
More file actions
55 lines (37 loc) · 835 Bytes
/
Jquery.js
File metadata and controls
55 lines (37 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//: 23.1 => Prefix Jquery object variables with a $.
// bad
const sidebar = $(".sidebar");
// good
const $sidebar = $(".sidebar");
// good
const $sidebarBtn = $(".sidebar-btn");
//: 23.2 => Cache jQuery lookups.
// bad
function setSidebar() {
$(".sidebar").hide();
//...
$(".sidebar").css({
"background-color": "pink",
});
}
// good
function setSidebar() {
const sidebar = $(".sidebar");
$sidebar.hide();
//...
$sidebar.css({
"background-color": "pink",
});
}
//: 23.3 => For DOM queries use Cascading $(".sidebar ul") or parent > child $(".sidebar > ul")
//: 23.4 => Use "find" with scoped Jquery object queries
// bad
$('ul', '.sidebar').hide();
// bad
$('.sidebar').find('ul').hide();
// good
$('.sidebar ul').hide();
// good
$('.sidebar > ul').hide();
// good
$sidebar.find('ul').hide();