Skip to content

Commit c70d4bd

Browse files
bobholtajpiano
authored andcommitted
apply core styles to performance section, site-wide tweaks
1 parent d244c32 commit c70d4bd

29 files changed

+120
-117
lines changed

page/about-jquery/how-jquery-works.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ $( document ).ready( function() {
5757
Inside the ready event, add a click handler to the link:
5858

5959
```
60-
$( document ).ready(function(){
60+
$( document ).ready(function() {
6161
6262
$("a").click(function( event ) {
6363
@@ -106,7 +106,7 @@ element's src attribute
106106
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
107107
<script>
108108
109-
$(document).ready(function() {
109+
$( document ).ready(function() {
110110
111111
$("a").click(function( event ) {
112112

page/code-organization/concepts.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ some concepts that are common to all good code organization patterns.
1919

2020
- Your code should be divided into units of functionality — modules, services,
2121
etc. Avoid the temptation to have all of your code in one huge
22-
`$(document).ready()` block. This concept, loosely, is known as
22+
`$( document ).ready()` block. This concept, loosely, is known as
2323
encapsulation.
2424
- Don't repeat yourself. Identify similarities among pieces of functionality,
2525
and use inheritance techniques to avoid repetitive code.

page/code-organization/feature-browser-detection.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ There are several ways to go about feature detection:
5858

5959
Let's take a look at how to check whether or not a `<canvas>` element exists in a specific browser, without using a helper library. We do this by specifically querying whether the method or property exists:
6060

61-
```js
61+
```
6262
// We want to show a graph in browsers that support canvas, but a data table in browsers that don't.
6363
var elem = document.createElement("canvas");
6464
@@ -93,7 +93,7 @@ Thankfully, there are some great helper libraries (like [Modernizr](http://moder
9393

9494
For example, utilizing Modernizr, we are able to do the same canvas detection test with this code:
9595

96-
```js
96+
```
9797
if ( Modernizr.canvas ) {
9898
9999
showGraphWithCanvas();
@@ -113,7 +113,7 @@ So, while the Modernizr syntax is great, it can end up being quite cumbersome to
113113

114114
The Modernizr object exposes a `load()` method that many prefer over the syntax mentioned previously. This is due to the another library that Modernizr now uses internally: [yepnope](http://yepnopejs.com/). Testing for canvas can now become something like this:
115115

116-
```js
116+
```
117117
Modernizr.load({
118118
test: Modernizr.canvas,
119119
yep : 'canvas.js',

page/effects/queue_and_dequeue_explained.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ $.fn.pause = function( n ) {
5656
5757
var el = this;
5858
59-
setTimeout( function () {
59+
setTimeout( function() {
6060
6161
return $( el ).dequeue();
6262

page/effects/uses_of_queue_and_dequeue.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $.fn.delay = function( time, type ) {
4141
The default queue in jQuery is fx. The default queue has some special
4242
properties that are not shared with other queues.
4343

44-
- Auto Start: When calling `$(elem).queue(function(){});` the fx queue will
44+
- Auto Start: When calling `$(elem).queue(function() {});` the fx queue will
4545
automatically dequeue the next function and run it if the queue hasn’t
4646
started.
4747
- ‘inprogress’ sentinel: Whenever you `dequeue()` a function from the fx queue,

page/events/event-delegation.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $("#list a").on( "click", function(event) {
4040
4141
event.preventDefault();
4242
43-
console.log( $(this).text() );
43+
console.log( $( this ).text() );
4444
4545
});
4646
```
@@ -72,7 +72,7 @@ $("#list").on("click", "a", function(event) {
7272
7373
event.preventDefault();
7474
75-
console.log( $(this).text() );
75+
console.log( $( this ).text() );
7676
7777
});
7878
```

page/events/inside-event-handling-function.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Stop the event from bubbling up to other elements.
4242
In addition to the event object, the event handling function also has access to
4343
the DOM element that the handler was bound to via the keyword this. To turn
4444
the DOM element into a jQuery object that we can use jQuery methods on, we
45-
simply do $(this), often following this idiom:
45+
simply do $( this ), often following this idiom:
4646

4747
```
4848
var $this = $( this );

page/events/introduction-to-custom-events.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ the behavior of a lightbulb belongs to the lightbulbs.
165165
If you&apos;re accustomed to object-oriented programming, you may find it useful to
166166
think of custom events as methods of objects. Loosely speaking, the object to
167167
which the method belongs is created via the jQuery selector. Binding the
168-
changeState custom event to all `$('.light')` elements is akin to having a
168+
changeState custom event to all `$(".light")` elements is akin to having a
169169
class called `Light` with a method of `changeState`, and then instantiating new
170170
`Light` objects for each element with a classname of light.
171171

page/events/introduction-to-events.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $("#helloBtn").click(function(event) {
6565
});
6666
```
6767

68-
The `$('#helloBtn')` code selects the button element using the `$` (aka `jQuery`) function and returns a jQuery object. The jQuery object has a bunch of methods (functions) available to it, one of them named `click`, which resides in the jQuery object's prototype. We call the `click` method on the jQuery object and pass along an anonymous function event handler that's going to be executed when a user clicks the button, alerting "Hello." to the user.
68+
The `$("#helloBtn")` code selects the button element using the `$` (aka `jQuery`) function and returns a jQuery object. The jQuery object has a bunch of methods (functions) available to it, one of them named `click`, which resides in the jQuery object's prototype. We call the `click` method on the jQuery object and pass along an anonymous function event handler that's going to be executed when a user clicks the button, alerting "Hello." to the user.
6969

7070
There are a number of ways that events can be listened for using jQuery:
7171

@@ -169,7 +169,7 @@ Finally, to inspect the event itself and see all of the data it contains, you sh
169169

170170
```
171171
//Logging an event's information
172-
$('form').on( 'submit', function(event) {
172+
$("form").on( "submit", function(event) {
173173
174174
event.preventDefault(); // Prevent the form's default submission.
175175

page/events/working_with_events_part_1.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ The general idea of event delegation is to bind the event handler to a
124124
containing element and then have an action take place based on which specific
125125
element within that containing element is targeted. Let's say we have another
126126
unordered list: `<ul id="list2"> ... </ul>`. Instead of attaching the .click()
127-
method to a button — $('#list2 li.special button').click(...) — we can attach
127+
method to a button — $("#list2 li.special button").click(...) — we can attach
128128
it to the entire surrounding `<ul>`. Through the magic of "bubbling," any click
129129
on the button is also a click on the button's surrounding list item, the list
130130
as a whole, the containing div, and all the way up to the window object. Since

page/faq/add_keyboard_navigation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ In Mathieu’s code, he had the following:
170170
});
171171
```
172172

173-
Note that $().ready is the same as $(document).ready (though it’s not a style I would personally use). *Directly* after the plugin (i.e. before the });) results in the following code:
173+
Note that $().ready is the same as $( document ).ready (though it’s not a style I would personally use). *Directly* after the plugin (i.e. before the });) results in the following code:
174174

175175
```
176176
$().ready(function() {

page/faq/enable_the_back_button.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Now armed with Ben’s plugin, we’re going to refactor the tab code so that th
2222

2323
The way the existing tabs work is as follows:
2424

25-
- Collect all the tab panels using $('div.tabs > div') and initialise to show the first tab1
25+
- Collect all the tab panels using $("div.tabs > div") and initialise to show the first tab1
2626
- Listen for clicks on the links that form the actual tabs
2727
- When a tab is clicked, hide all the tab panels, filter down to the one we wanted to see and show it, then update the classes on the tabs so the current link appears to be focused
2828
- Finally, initialise by finding the first tab and triggering a click
@@ -34,7 +34,7 @@ This process was our original code, and most of it needs to stay in place. The c
3434
So we listen for the hashchange event, just like we might listen for a click event:
3535

3636
```
37-
$( window ).bind("hashchange", function(){
37+
$( window ).bind("hashchange", function() {
3838
3939
//do some magic
4040
@@ -44,7 +44,7 @@ $( window ).bind("hashchange", function(){
4444
Now we move all of the original code from the click handler in to this new event listener. I’ve copied this code in, and marked in bold which lines we’ll need to change:
4545

4646
```
47-
$( window ).bind( "hashchange", function(){
47+
$( window ).bind( "hashchange", function() {
4848
4949
tabContainers.hide();
5050
@@ -88,7 +88,7 @@ $(function() {
8888
8989
tabContainers.hide().filter(":first").show();
9090
91-
$( window ).bind( "hashchange", function(){
91+
$( window ).bind( "hashchange", function() {
9292
9393
var hash = window.location.hash || "#first";
9494

page/faq/how_do_i_test_whether_an_element_has_a_particular_class.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ source: http://docs.jquery.com/Frequently_Asked_Questions
66
[hasClass](http://api.jquery.com/hasClass/) (added in version 1.2) handles this common use case:
77

88
```
9-
$("div").click(function(){
9+
$("div").click(function() {
1010
11-
if ( $(this).hasClass("protected") ) {
11+
if ( $( this ).hasClass("protected") ) {
1212
1313
$( this )
1414
.animate({ left: -10 })

page/javascript-101/closures.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ by creating a bound function using the original object as demonstrated below.
148148
var user = "johnsmith";
149149
var module = {
150150
151-
getUser: function(){
151+
getUser: function() {
152152
153153
return this.user;
154154

page/javascript-101/syntax-basics.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var foo = function() {
4949
foo();
5050
5151
// not so much!
52-
var foo=function(){for(var i=0;i<10;++){alert(i);}};foo();
52+
var foo=function() {for(var i=0;i<10;++){alert(i);}};foo();
5353
```
5454

5555
### Reserved Words

page/javascript-101/this-keyword.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ myObject.sayHello.call( secondObject ); // logs "Hi! My name is Colin"
4949
// A function created using Function.bind
5050
var myName = "the global object",
5151
52-
sayHello = function () {
52+
sayHello = function() {
5353
5454
console.log( "Hi! My name is " + this.myName );
5555

page/performance/append-outside-loop.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ Touching the DOM comes at a cost; if you're appending a lot of elements to the
1010
DOM, you will want to append them all at once, rather then one at a time. This is common problem when appending elements within a loop.
1111

1212
```
13-
$.each(myArray, function(i, item) {
14-
var newListItem = '<li>' + item + '</li>';
15-
$('#ballers').append(newListItem);
13+
$.each( myArray, function( i, item ) {
14+
15+
var newListItem = "<li>" + item + "</li>";
16+
17+
$("#ballers").append( newListItem );
18+
1619
});
1720
```
1821

@@ -23,26 +26,32 @@ element. After the loop, just append the fragment to the DOM element.
2326
```
2427
var frag = document.createDocumentFragment();
2528
26-
$.each(myArray, function (i, item) {
29+
$.each( myArray, function( i, item ) {
30+
2731
var newListItem = document.createElement("li");
28-
var itemText = document.createTextNode(item);
29-
newListItem.appendChild(itemText);
30-
frag.appendChild(newListItem);
32+
var itemText = document.createTextNode( item );
33+
34+
newListItem.appendChild( itemText );
35+
36+
frag.appendChild( newListItem );
37+
3138
});
3239
33-
$('#ballers')[0].appendChild(frag);
40+
$("#ballers")[ 0 ].appendChild( frag );
3441
```
3542

3643
Another technique, which is quite simple, is to build up a string during each iteration of the loop. After the loop, just set the html of the DOM element to that string.
3744

3845
```
39-
var myHtml = '';
46+
var myHtml = "";
47+
48+
$.each( myArray, function( i, item ) {
49+
50+
myHtml += "<li>" + item + "</li>";
4051
41-
$.each(myArray, function(i, item) {
42-
myHtml += '<li>' + item + '</li>';
4352
});
4453
45-
$('#ballers').html(myHtml);
54+
$("#ballers").html( myHtml );
4655
```
4756

4857
There are of course other techniques you could certainly test out; a great way to test the performance of these is through a site called [jsperf](http://jsperf.com). This site allows you to benchmark each technique and visually see how it performs across all the browsers.

page/performance/cache-loop-length.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ it beforehand.
1212
```
1313
var myLength = myArray.length;
1414
15-
for (var i = 0; i < myLength; i++) {
15+
for ( var i = 0; i < myLength; i++ ) {
16+
1617
// do stuff
18+
1719
}
1820
```

page/performance/clever-conditionals.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ attribution:
88

99
```
1010
// old way
11-
if (type == 'foo' || type == 'bar') { ... }
11+
if ( type == "foo" || type == "bar" ) { ... }
1212
1313
// better
14-
if (/^(foo|bar)$/.test(type)) { ... }
14+
if ( /^(foo|bar)$/.test(type) ) { ... }
1515
1616
// object literal lookup
17-
if (({ foo : 1, bar : 1 })[type]) { ... }
17+
if ( ({ foo : 1, bar : 1 })[ type ] ) { ... }
1818
```

page/performance/detach-elements-before-work-with-them.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ introduced `$.fn.detach` in version 1.4 to help address this issue, allowing you
1111
to remove an element from the DOM while you work with it.
1212

1313
```
14-
var $table = $('#myTable');
14+
var $table = $("#myTable");
1515
var $parent = $table.parent();
1616
1717
$table.detach();
18+
1819
// ... add lots and lots of rows to table
19-
$parent.append(table);
20+
$parent.append( table );
2021
```

page/performance/dont-act-on-absent-elements.md

+19-9
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,30 @@ that your selection contains some elements.
1414
// BAD: this runs three functions
1515
// before it realizes there's nothing
1616
// in the selection
17-
$('#nosuchthing').slideUp();
17+
$("#nosuchthing").slideUp();
1818
1919
// Better
20-
var $mySelection = $('#nosuchthing');
21-
if ($mySelection.length) { $mySelection.slideUp(); }
20+
var $mySelection = $("#nosuchthing");
21+
22+
if ( $mySelection.length ) {
23+
24+
$mySelection.slideUp();
25+
26+
}
2227
2328
// BEST: add a doOnce plugin
24-
jQuery.fn.doOnce = function(func){
25-
this.length && func.apply(this);
26-
return this;
27-

}
29+
jQuery.fn.doOnce = function( func ){
30+
31+
this.length && func.apply( this );
32+
33+
return this;
34+
35+
}
36+
37+
$("li.cartitems").doOnce(function() {

38+
39+
// make it ajax! \o/

2840
29-

$('li.cartitems').doOnce(function(){

30-
// make it ajax! \o/

3141
});
3242
```
3343

0 commit comments

Comments
 (0)