Skip to content

Commit bb10b89

Browse files
jorydotcomajpiano
jorydotcom
authored andcommitted
Style Guide conformance for About jQuery & JavaScript 101 sections. Fixes jquery#183.
1 parent cb41fed commit bb10b89

15 files changed

+242
-387
lines changed

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

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ can use:
3131
<script src="jquery.js"></script>
3232
```
3333

34-
You can download your own copy of jQuery from the [Downloading jQuery](http://jquery.com/download/) page
34+
Download a copy of jQuery from the [Downloading jQuery](http://jquery.com/download/) page.
3535

3636
### Launching Code on Document Ready
3737
The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:
@@ -42,7 +42,7 @@ window.onload = function() {
4242
4343
}
4444
```
45-
Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is that the HTML 'document' isn't finished loading yet, when you first try to run your code.
45+
Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using `window.onload` in the first place is that the HTML 'document' isn't finished loading yet, when you first try to run your code.
4646

4747
To circumvent both problems, jQuery has a simple statement that checks the `document` and waits until it's ready to be manipulated, known as the [ ready event ](http://api.jquery.com/ready):
4848

@@ -91,11 +91,11 @@ The following is an example of what the complete HTML file might look like if
9191
you were to use the script in your own file. Note that it links to Google's
9292
[CDN](http://code.google.com/apis/libraries/) to load the jQuery core file.
9393
Also, while the custom script is included in the `<head>`, it is generally
94-
preferable to place it in a separate file and refer that file with the script
95-
element's src attribute
94+
preferable to place it in a separate file and refer to that file with the script
95+
element's `src` attribute.
9696

9797
```
98-
<!DOCTYPE html>
98+
<!doctype html>
9999
<html lang="en">
100100
<head>
101101
<meta charset="utf-8">
@@ -129,7 +129,7 @@ element's src attribute
129129

130130
Another common task is adding (or removing) a `class`.
131131

132-
First, add some style information into the `head` of your document, like this:
132+
First, add some style information into the `head` of the document, like this:
133133

134134
```
135135
<style>
@@ -139,15 +139,15 @@ First, add some style information into the `head` of your document, like this:
139139
</style>
140140
```
141141

142-
Next, add the [addClass](http://api.jquery.com/addClass) call to your script:
142+
Next, add the [addClass](http://api.jquery.com/addClass) call to the script:
143143

144144
```
145145
$("a").addClass("test");
146146
```
147147

148-
All your `a` elements will now be bold.
148+
All `a` elements will now be bold.
149149

150-
To remove the `class`, use [removeClass](http://api.jquery.com/removeClass)
150+
To remove the `class`, use [removeClass](http://api.jquery.com/removeClass):
151151

152152
```
153153
$("a").removeClass("test");
@@ -156,8 +156,8 @@ $("a").removeClass("test");
156156
### Special Effects
157157

158158
In jQuery, a couple of handy [effects](http://api.jquery.com/category/effects/)
159-
are provided, to really make your web site stand out. To put this to the test,
160-
change the click that you added earlier to this:
159+
are provided, to really make web sites stand out. For example,
160+
change the click you added earlier to the following:
161161

162162
```
163163
$("a").click(function( event ){
@@ -169,29 +169,29 @@ $("a").click(function( event ){
169169
});
170170
```
171171

172-
Now, if you click any link, it should make itself slowly disappear.
172+
Now, the link should slowly disappear when clicked.
173173

174-
## Callback and Functions
174+
## Callbacks and Functions
175175

176176
A callback is a function that is passed as an argument to another function and
177177
is executed after its parent function has completed. The special thing about a
178178
callback is that functions that appear after the "parent" can execute before
179179
the callback executes. Another important thing to know is how to properly pass
180180
the callback.
181181

182-
### Callback *without* arguments
182+
### Callback *without* Arguments
183183

184-
For a callback with no arguments you pass it like this:
184+
Pass callbacks with no arguments like this:
185185

186186
```
187187
$.get( "myhtmlpage.html", myCallBack );
188188
```
189189

190-
**Note** that the second parameter here is simply the function name (but *not* as a string and without parentheses). Functions in Javascript are 'First class citizens' and so can be passed around like variable references and executed at a later time.
190+
**Note** that the second parameter here is simply the function name (but *not* as a string and without parentheses). Functions in Javascript are 'First class citizens' and can be passed around like variable references and executed at a later time.
191191

192-
### Callback *with* arguments
192+
### Callback *with* Arguments
193193

194-
"What do you do if you have arguments that you want to pass?", you might ask yourself.
194+
Executing callbacks with arguments can be tricky. The below example illustrates how to think about callbacks with arguments:
195195

196196
#### Wrong
197197
The Wrong Way (will ***not*** work!)
@@ -201,18 +201,15 @@ $.get( "myhtmlpage.html", myCallBack(param1, param2) );
201201
```
202202

203203

204-
This will not work because it calls `myCallBack( param1, param2 )`
205-
206-
207-
and then passes the return value as the second parameter to [$.get()](http://api.jquery.com/jQuery.get/)
204+
This will not work because it calls `myCallBack( param1, param2 )` and then passes the return value as the second parameter to [$.get()](http://api.jquery.com/jQuery.get/).
208205

209206
#### Right
210207

211208
The problem with the above example is that `myCallBack( param1, param2 )` is
212-
evaluated before being passed as a function. Javascript and by extension jQuery
213-
expects a function pointer in cases like these, e.g., `setTimeout( function() {}, 100)`
209+
evaluated before being passed as a function. Javascript (and by extension, jQuery)
210+
expects a function pointer in cases like these, e.g., `setTimeout( function() {}, 100)`.
214211

215-
In the below usage, an anonymous function is created (just a block of
212+
In the example below, an anonymous function is created (just a block of
216213
statements) and is registered as the callback function. Note the use of
217214
`function() {`. The anonymous function does exactly one thing: calls
218215
`myCallBack`, with the values of `param1` and `param2` from the outer scope.

page/javascript-101/arrays.md

Lines changed: 41 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,19 @@ source: http://jqfundamentals.com/legacy
55
attribution:
66
- jQuery Fundamentals
77
---
8-
Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of
9-
related items of the same type (such as strings), though in reality, an array
10-
can include multiple types of items, including other arrays.
8+
Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of related items of the same type (such as strings), though in reality, an array can include multiple types of items, including other arrays.
119

12-
To create an array you can either use the object constructor or the literal declaration,
13-
by assigning your variable a list of values right after the declaration.
10+
To create an array, either use the object constructor or the literal declaration, by assigning the variable a list of values after the declaration.
1411

1512
```
1613
// A simple array
1714
var myArray1 = new Array( "hello", "world" ); // with constructor
1815
var myArray2 = [ "hello", "world" ]; // literal declaration, the preferred way
1916
```
2017

21-
The literal declaration is preferred, see the
22-
[Google Coding Guidelines](http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Array_and_Object_literals)
23-
for more information.
24-
25-
If you don't know your values yet, it is also possible to declare an empty Array, and
26-
add elements either through functions or through accessing by index:
18+
The literal declaration is generally preferred. See the [Google Coding Guidelines](http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Array_and_Object_literals) for more information.
2719

20+
If the values are unknown, it is also possible to declare an empty Array, and add elements either through functions or through accessing by index:
2821

2922
```
3023
// Creating empty arrays and adding values
@@ -37,9 +30,7 @@ myArray.push("world"); // adds "world" on index 1
3730
myArray[ 2 ] = "!"; // adds "!" on index 2
3831
```
3932

40-
'push' is a function which adds an element on the end of the array and expands the array
41-
respectively. You also can directly add items by index. Missing indices will be filled
42-
with 'undefined';
33+
'push' is a function that adds an element on the end of the array and expands the array respectively. You also can directly add items by index. Missing indices will be filled with 'undefined'.
4334

4435
```
4536
// Leaving indices
@@ -52,21 +43,19 @@ myArray[ 3 ] = "!";
5243
console.log( myArray ); // logs [ "hello", "world", undefined, "!" ];
5344
```
5445

55-
So 'push' is far more safe, especially if you don't know the size of your
56-
array yet. With the index you not only assign values to array items, but also
57-
access those.
46+
If the size of the array is unknown, 'push' is far more safe. You can both access and assign values to array items with the index.
5847

5948
```
6049
// Accessing array items by index
6150
var myArray = [ "hello", "world", "!" ];
6251
console.log( myArray[2] ); // logs "!"
6352
```
6453

65-
### Array methods and properties
54+
## Array Methods and Properties
6655

67-
## length
56+
### `.length`
6857

69-
The 'length' property is used to know the amount of items in your array.
58+
The `.length` property is used to determine the amount of items in an array.
7059

7160
```
7261
// Length of an array
@@ -75,7 +64,7 @@ var myArray = [ "hello", "world", "!" ];
7564
console.log( myArray.length ); // logs 3
7665
```
7766

78-
You will need the length property for looping through an array:
67+
You will need the `.length` property for looping through an array:
7968

8069
```
8170
// For loops and arrays - a classic
@@ -88,7 +77,7 @@ for ( var i = 0; i < myArray.length; i = i + 1 ) {
8877
}
8978
```
9079

91-
Except when you are using for ... in loops:
80+
Except when using `for`/`in` loops:
9281

9382
```
9483
// For loops and arrays - alternate method
@@ -101,9 +90,9 @@ for ( var i in myArray ) {
10190
}
10291
```
10392

104-
## concat
93+
### `.concat`
10594

106-
With 'concat', you can concatenate two or more arrays
95+
Concatenate two or more arrays with `.concat`:
10796

10897
```
10998
// Concatenating Arrays
@@ -112,10 +101,9 @@ var myOtherArray = [ 5, 6, 7 ];
112101
var wholeArray = myArray.concat( myOtherArray ); // [ 2, 3, 4, 5, 6, 7 ]
113102
```
114103

115-
## join
104+
### `.join`
116105

117-
'join' creates a string representation of your array. It's parameter is as string
118-
which works as a separator between elements (default is a comma);
106+
`.join` creates a string representation of the array. Its parameter is a string that works as a separator between elements (default separator is a comma):
119107

120108
```
121109
// Joining elements
@@ -127,12 +115,12 @@ console.log( myArray.join() ); // logs "hello,world,!"
127115
128116
console.log( myArray.join("") ); // logs "helloworld!"
129117
130-
console.log( myArray.join("!!") ); // logs "hello!!world!!!!!";
118+
console.log( myArray.join("!!") ); // logs "hello!!world!!!";
131119
```
132120

133-
## pop
121+
### `.pop`
134122

135-
'pop' removes the last element of an array. It is the opposite method to 'push'
123+
`.pop` removes the last element of an array. It is the opposite method of `.push`:
136124

137125
```
138126
// pushing and popping
@@ -147,10 +135,9 @@ myArray.push( 7 ); // [ 0 , 2 , 7 ]
147135
myArray.pop(); // [ 0 , 2 ]
148136
```
149137

150-
## reverse
138+
### `.reverse`
151139

152-
As the name suggests, the elements of the array are in reverse order after calling
153-
this method
140+
As the name suggests, the elements of the array are in reverse order after calling this method:
154141

155142
```
156143
// reverse
@@ -159,10 +146,9 @@ var myArray = [ "world" , "hello" ];
159146
myArray.reverse(); // [ "hello", "world" ]
160147
```
161148

162-
## shift
149+
### `.shift`
163150

164-
Removes the first element of an array. With 'pop' and 'shift' you can recreate the
165-
method of a [queue](http://en.wikipedia.org/wiki/Queue_(data_structure))
151+
Removes the first element of an array. With `.pop` and `.shift`, you can recreate the method of a [queue](http://en.wikipedia.org/wiki/Queue_(data_structure)):
166152

167153
```
168154
// queue with shift() and pop()
@@ -177,10 +163,9 @@ myArray.push( 7 ); // [ 0 , 2 , 7 ]
177163
myArray.shift(); // [ 2 , 7 ]
178164
```
179165

180-
## slice
166+
### `.slice`
181167

182-
Extracts a part of the array and returns them in a new one. This method takes one
183-
parameter, which is the starting index.
168+
Extracts a part of the array and returns that part in a new array. This method takes one parameter, which is the starting index:
184169

185170
```
186171
// slicing
@@ -191,19 +176,18 @@ console.log( myArray ); // [ 1, 2, 3, 4, 5, 6, 7, 8 ]
191176
console.log( newArray ); // [ 4, 5, 6, 7, 8 ]
192177
```
193178

194-
## splice
179+
### `.splice`
195180

196-
Removes a certain amount of elements and adds new ones at the given index. It takes
197-
at least 3 parameters
181+
Removes a certain amount of elements and adds new ones at the given index. It takes at least 3 parameters:
198182

199183
```
200184
// splice method
201-
myArray.splice( idx, len, values, ... );
185+
myArray.splice( index, length, values, ... );
202186
```
203187

204-
* idx = the starting index
205-
* len = the number of elements to remove
206-
* values = the values which should be inserted at idx
188+
* *Index* - The starting index.
189+
* *Length* - The number of elements to remove.
190+
* *Values* - The values to be inserted at idx.
207191

208192
For example:
209193

@@ -214,10 +198,9 @@ myArray.splice( 1, 2, 1, 2, 3, 4 );
214198
console.log( myArray ); // [ 0, 1, 2, 3, 4, 5 ]
215199
```
216200

217-
## sort
201+
### `.sort`
218202

219-
Sorts an array. It takes one parameter, which is a comparing function. If this function is not
220-
given, the array is sorted ascending
203+
Sorts an array. It takes one parameter, which is a comparing function. If this function is not given, the array is sorted ascending:
221204

222205
```
223206
// sorting without comparing function
@@ -239,13 +222,11 @@ var myArray = [ 3, 4, 6, 1 ];
239222
myArray.sort( descending ); // [ 6, 4, 3, 1 ]
240223
```
241224

242-
The return value of descending (for this example) is important. If the return value is
243-
less than zero, the index of a is before b, and if it is greater than zero it's vice-versa.
244-
If the return value is zero, the elements index is equal.
225+
The return value of descending (for this example) is important. If the return value is less than zero, the index of a is before b, and if it is greater than zero it's vice-versa. If the return value is zero, the elements index is equal.
245226

246-
## unshift
227+
### `.unshift`
247228

248-
Inserts an element at the first position of the array
229+
Inserts an element at the first position of the array:
249230

250231
```
251232
// unshift
@@ -258,18 +239,16 @@ myArray.unshift( 2 ); // [ 2 , 0 ]
258239
myArray.unshift( 7 ); // [ 7 , 2 , 0 ]
259240
```
260241

261-
## forEach
242+
### `.forEach`
262243

263-
In modern browsers, like Chrome, Firefox and Internet Explorer 9 it is possible to traverse
264-
through arrays by a so called 'forEach' method, where you pass a function which is called
265-
for each element in your array.
244+
In modern browsers it is possible to traverse through arrays with a `.forEach` method, where you pass a function that is called for each element in the array.
266245

267246
The function takes up to three arguments:
268-
* element - The element itself
269-
* index - The index of this element in the array
270-
* array - The array itself
247+
* *Element* - The element itself.
248+
* *Index* - The index of this element in the array.
249+
* *Array* - The array itself.
271250

272-
All of the are optional, but you will need at least the 'element' parameter in most cases.
251+
All of these are optional, but you will need at least the 'element' parameter in most cases.
273252

274253
```
275254
// native forEach

0 commit comments

Comments
 (0)