Skip to content

Commit 2093d0c

Browse files
author
James Halliday
committed
repl, constructors
1 parent a0dd3aa commit 2093d0c

File tree

1 file changed

+170
-9
lines changed

1 file changed

+170
-9
lines changed

javascript.markdown

+170-9
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ numbers:
133133
modulo is really handy when you need a value
134134
to "wrap around"
135135
---
136+
# the REPL
137+
138+
There is another way of evaluating expressions as you type
139+
them in: the REPL.
140+
141+
The REPL is like calculator mode.
142+
143+
Just type `node` to get into a REPL.
144+
145+
Then type an expression and you will see its result:
146+
147+
```
148+
> 1+2
149+
3
150+
```
151+
152+
Type ctrl+d to exit the REPL.
153+
---
136154
# assignment updates
137155

138156
You can change the value stored in a variable
@@ -1072,23 +1090,171 @@ function add (a, b) {
10721090
}
10731091
console.log(binary(3, 4, add));
10741092
```
1093+
---
1094+
# higher-order functions: inline version
1095+
1096+
We could write the previous example with the `add` function
1097+
declared inline:
10751098
1099+
``` js
1100+
function binary (a, b, operator) {
1101+
return operator(a, b);
1102+
}
1103+
console.log(binary(3, 4, function (a, b) {
1104+
return a + b;
1105+
}));
1106+
```
10761107
---
1077-
# return values
1108+
# higher-order functions: everything inline why not
1109+
1110+
We could even define the binary function inline, although
1111+
this is not very readable:
1112+
1113+
``` js
1114+
console.log(
1115+
function (a, b, operator) {
1116+
return operator(a, b);
1117+
}(3, 4, function (a, b) {
1118+
return a + b;
1119+
})
1120+
);
1121+
```
1122+
---
1123+
# higher-order functions: Array.prototype.map
1124+
1125+
There are some built-in functions that accept functions as
1126+
arguments. `.map()` takes a function that transforms the
1127+
contents of an array, creating a new array:
1128+
1129+
``` js
1130+
var first = [ 3, 4, 5 ];
1131+
var second = first.map(plusFifty);
1132+
console.log(second);
1133+
1134+
function plusFifty (x) { return x + 50 }
1135+
```
1136+
1137+
prints:
1138+
1139+
```
1140+
[ 53, 54, 55 ]
1141+
```
1142+
---
1143+
# higher-order functions: Array.prototype.forEach
1144+
1145+
You can also use `forEach` to loop over items in an array
1146+
without having to whip up a `for` loop:
1147+
1148+
``` js
1149+
[ 7, 8, 9 ].forEach(function (x) {
1150+
console.log(x);
1151+
});
1152+
```
1153+
---
1154+
# returning a function
10781155
10791156
Functions can return any javascript value: numbers, strings,
10801157
arrays, objects, even other functions!
10811158
1159+
When you return a function from another function, the
1160+
variables you declare persist in the inner function.
1161+
1162+
``` js
1163+
function counter () {
1164+
var times = 0;
1165+
return function () {
1166+
times ++;
1167+
return times;
1168+
};
1169+
}
1170+
1171+
var c = counter(); // c is a function
1172+
console.log(c()); // 1
1173+
console.log(c()); // 2
1174+
console.log(c()); // 3
1175+
```
10821176
---
1083-
# inline functions
1177+
# returning an object with functions
1178+
1179+
Another common pattern is to return an object with functions
1180+
as values:
1181+
1182+
```
1183+
function Num (value) {
1184+
return {
1185+
add: function (x) {
1186+
value += x;
1187+
return x;
1188+
},
1189+
multiply: function (x) {
1190+
value *= x;
1191+
return x;
1192+
};
1193+
};
1194+
}
1195+
1196+
var n = Num(100);
1197+
console.log(n.add(5)); // 105
1198+
console.log(n.multiply(3)); // 315
1199+
```
10841200
1085-
You can define a function
10861201
---
10871202
# constructors
10881203
1089-
Constructors are functions for pumping out instances.
1204+
Another way to create objects with properties is to use a
1205+
constructor. The previous example is pretty much the same
1206+
as:
1207+
1208+
``` js
1209+
function Num (value) {
1210+
this.value = value;
1211+
}
1212+
1213+
Num.prototype.add = function (x) {
1214+
this.value += x;
1215+
return this.value;
1216+
}
1217+
1218+
Num.prototype.multiply = function (x) {
1219+
this.value *= x;
1220+
return this.value;
1221+
}
10901222

1223+
var n = new Num(100);
1224+
console.log(n.add(5)); // 105
1225+
console.log(n.multiply(3)); // 315
1226+
```
10911227
1228+
`n` is an "instance" of Num. You can make as many instances
1229+
as you like and they will all have separate values.
1230+
---
1231+
# constructors: new trick
1232+
1233+
If you use a constructor, don't forget to use `new`!
1234+
1235+
Here is one weird trick discovered by a bus driver for
1236+
making constructor instances without `new`:
1237+
1238+
``` js
1239+
function Num (value) {
1240+
if (!(this instanceof Num)) return new Num(value);
1241+
this.value = value;
1242+
}
1243+
1244+
Num.prototype.add = function (x) {
1245+
this.value += x;
1246+
return this.value;
1247+
}
1248+
1249+
Num.prototype.multiply = function (x) {
1250+
this.value *= x;
1251+
return this.value;
1252+
}
1253+
1254+
var n = Num(100); // you don't need to remember `new` now
1255+
console.log(n.add(5)); // 105
1256+
console.log(n.multiply(3)); // 315
1257+
```
10921258
---
10931259
# builtins
10941260
@@ -1225,11 +1391,6 @@ console.log([ d.getFullYear(), d.getMonth(), d.getDay() ])
12251391

12261392
console.log([ d.getHours(), d.getMinutes(), d.getSeconds() ])
12271393
// [ 10, 18, 26 ]
1228-
---
1229-
# the REPL
1230-
1231-
---
1232-
12331394
---
12341395
# homework
12351396

0 commit comments

Comments
 (0)