Skip to content

Commit a582d14

Browse files
committed
Merge pull request BonsaiDen#271 from chrisranderson/master
change 'that' to 'self', rename some 'foobar' examples
2 parents 1f91a84 + f5bd48b commit a582d14

File tree

4 files changed

+62
-60
lines changed

4 files changed

+62
-60
lines changed

doc/en/array/general.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ While the *getter* of the `length` property simply returns the number of
4040
elements that are contained in the array, the *setter* can be used to
4141
**truncate** the array.
4242

43-
var foo = [1, 2, 3, 4, 5, 6];
44-
foo.length = 3;
45-
foo; // [1, 2, 3]
43+
var arr = [1, 2, 3, 4, 5, 6];
44+
arr.length = 3;
45+
arr; // [1, 2, 3]
4646

47-
foo.length = 6;
48-
foo.push(4);
49-
foo; // [1, 2, 3, undefined, undefined, undefined, 4]
47+
arr.length = 6;
48+
arr.push(4);
49+
arr; // [1, 2, 3, undefined, undefined, undefined, 4]
5050

5151
Assigning a smaller length truncates the array. Increasing it creates a sparse array.
5252

doc/en/core/eval.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
The `eval` function will execute a string of JavaScript code in the local scope.
44

5-
var foo = 1;
5+
var number = 1;
66
function test() {
7-
var foo = 2;
8-
eval('foo = 3');
9-
return foo;
7+
var number = 2;
8+
eval('number = 3');
9+
return number;
1010
}
1111
test(); // 3
12-
foo; // 1
12+
number; // 1
1313

1414
However, `eval` only executes in the local scope when it is being called
1515
directly *and* when the name of the called function is actually `eval`.
1616

17-
var foo = 1;
17+
var number = 1;
1818
function test() {
19-
var foo = 2;
20-
var bar = eval;
21-
bar('foo = 3');
22-
return foo;
19+
var number = 2;
20+
var copyOfEval = eval;
21+
copyOfEval('number = 3');
22+
return number;
2323
}
2424
test(); // 2
25-
foo; // 3
25+
number; // 3
2626

2727
The use of `eval` should be avoided. 99.9% of its "uses" can be achieved
2828
**without** it.

doc/en/function/constructors.md

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,74 +11,74 @@ constructor.
1111
If the function that was called has no explicit `return` statement, then it
1212
implicitly returns the value of `this` - the new object.
1313

14-
function Foo() {
15-
this.bla = 1;
14+
function Person(name) {
15+
this.name = name;
1616
}
1717

18-
Foo.prototype.test = function() {
19-
console.log(this.bla);
18+
Person.prototype.logName = function() {
19+
console.log(this.name);
2020
};
2121

22-
var test = new Foo();
22+
var sean = new Person();
2323

24-
The above calls `Foo` as constructor and sets the `prototype` of the newly
25-
created object to `Foo.prototype`.
24+
The above calls `Person` as constructor and sets the `prototype` of the newly
25+
created object to `Person.prototype`.
2626

2727
In case of an explicit `return` statement, the function returns the value
2828
specified by that statement, but **only** if the return value is an `Object`.
2929

30-
function Bar() {
31-
return 2;
30+
function Car() {
31+
return 'ford';
3232
}
33-
new Bar(); // a new object
33+
new Car(); // a new object, not 'ford'
3434

35-
function Test() {
36-
this.value = 2;
35+
function Person() {
36+
this.someValue = 2;
3737

3838
return {
39-
foo: 1
39+
name: 'Charles'
4040
};
4141
}
42-
new Test(); // the returned object
42+
new Test(); // the returned object ({name:'Charles'}), not including someValue
4343

4444
When the `new` keyword is omitted, the function will **not** return a new object.
4545

46-
function Foo() {
47-
this.bla = 1; // gets set on the global object
46+
function Pirate() {
47+
this.hasEyePatch = true; // gets set on the global object!
4848
}
49-
Foo(); // undefined
49+
var somePirate = Pirate(); // somePirate is undefined
5050

51-
While the above example might still appear to work in some cases, due to the
52-
workings of [`this`](#function.this) in JavaScript, it will use the
51+
While the above example might still appear to work in some cases, due to the
52+
workings of [`this`](#function.this) in JavaScript, it will use the
5353
*global object* as the value of `this`.
5454

5555
### Factories
5656

5757
In order to be able to omit the `new` keyword, the constructor function has to
5858
explicitly return a value.
5959

60-
function Bar() {
61-
var value = 1;
60+
function Robot() {
61+
var color = 'gray';
6262
return {
63-
method: function() {
64-
return value;
63+
getColor: function() {
64+
return color;
6565
}
6666
}
6767
}
68-
Bar.prototype = {
69-
foo: function() {}
68+
Robot.prototype = {
69+
someFunction: function() {}
7070
};
7171

72-
new Bar();
73-
Bar();
72+
new Robot();
73+
Robot();
7474

75-
Both calls to `Bar` return the same thing, a newly created object that
75+
Both calls to `Robot` return the same thing, a newly created object that
7676
has a property called `method`, which is a
7777
[Closure](#function.closures).
7878

79-
It should also be noted that the call `new Bar()` does **not** affect the
79+
It should also be noted that the call `new Robot()` does **not** affect the
8080
prototype of the returned object. While the prototype will be set on the newly
81-
created object, `Bar` never returns that new object.
81+
created object, `Robot` never returns that new object.
8282

8383
In the above example, there is no functional difference between using and
8484
not using the `new` keyword.
@@ -92,19 +92,21 @@ lead to bugs.
9292
In order to create a new object, one should rather use a factory and construct a
9393
new object inside of that factory.
9494

95-
function Foo() {
96-
var obj = {};
97-
obj.value = 'blub';
95+
function CarFactory() {
96+
var car = {};
97+
car.owner = 'nobody';
9898

99-
var private = 2;
100-
obj.someMethod = function(value) {
101-
this.value = value;
99+
var milesPerGallon = 2;
100+
101+
car.setOwner = function(newOwner) {
102+
this.owner = newOwner;
102103
}
103104

104-
obj.getPrivate = function() {
105-
return private;
105+
car.getMPG = function() {
106+
return milesPerGallon;
106107
}
107-
return obj;
108+
109+
return car;
108110
}
109111

110112
While the above is robust against a missing `new` keyword and certainly makes

doc/en/function/this.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ mis-design of the language because it **never** has any practical use.
6868
A common misconception is that `this` inside of `test` refers to `Foo`; while in
6969
fact, it **does not**.
7070

71-
In order to gain access to `Foo` from within `test`, it is necessary to create a
71+
In order to gain access to `Foo` from within `test`, you can create a
7272
local variable inside of `method` that refers to `Foo`.
7373

7474
Foo.method = function() {
75-
var that = this;
75+
var self = this;
7676
function test() {
77-
// Use that instead of this here
77+
// Use self instead of this here
7878
}
7979
test();
8080
}
8181

82-
`that` is just a normal variable name, but it is commonly used for the reference to an
82+
`self` is just a normal variable name, but it is commonly used for the reference to an
8383
outer `this`. In combination with [closures](#function.closures), it can also
8484
be used to pass `this` values around.
8585

0 commit comments

Comments
 (0)