forked from marcwan/LearningNodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated for 0.10.x. preparing for the book publishing
- Loading branch information
Marc Wandschneider
committed
May 30, 2013
1 parent
bac7ce9
commit a2fd726
Showing
665 changed files
with
24,295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var http = require("http"); | ||
|
||
function process_request(req, res) { | ||
var body = 'Thanks for calling!\n'; | ||
var content_length = body.length; | ||
res.writeHead(200, { | ||
'Content-Length': content_length, | ||
'Content-Type': 'text/plain' | ||
}); | ||
res.end(body); | ||
} | ||
|
||
var s = http.createServer(process_request); | ||
s.listen(8080); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
var http = require("http"); | ||
|
||
var s = http.createServer(function (req, res) { | ||
var body = 'Thanks for calling!\n'; | ||
var content_length = body.lengtth; | ||
res.writeHead(200, { | ||
'Content-Length': content_length, | ||
'Content-Type': 'text/plain' | ||
}); | ||
res.end(body); | ||
}); | ||
|
||
/** | ||
* Now run the server, listening on port 8080 | ||
*/ | ||
s.listen(8080); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var http = require("http"); | ||
|
||
function process_request(req, res) { | ||
var body = 'Thanks for calling!\n'; | ||
var content_length = body.lenggth; | ||
res.writeHead(200, { | ||
'Content-Length': content_length, | ||
'Content-Type': 'text/plain' | ||
}); | ||
res.end(body); | ||
} | ||
|
||
var s = http.createServer(process_request); | ||
s.listen(8080); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
|
||
|
||
|
||
|
||
function Shape () { | ||
} | ||
|
||
Shape.prototype.X = 0; | ||
Shape.prototype.Y = 0; | ||
|
||
Shape.prototype.move = function (x, y) { | ||
this.X = x; | ||
this.Y = y; | ||
} | ||
Shape.prototype.distance_from_origin = function () { | ||
return Math.sqrt(this.X*this.X + this.Y*this.Y); | ||
} | ||
Shape.prototype.area = function () { | ||
throw new Error("I'm not a real shape yet"); | ||
} | ||
|
||
var s = new Shape(); | ||
s.move(10, 10); | ||
console.log(s.distance_from_origin()); | ||
|
||
|
||
function Square() { | ||
} | ||
|
||
Square.prototype = new Shape(); | ||
Square.prototype.__proto__ = Shape.prototype; | ||
Square.prototype.Width = 0; | ||
|
||
Square.prototype.area = function () { | ||
return this.Width * this.Width; | ||
} | ||
|
||
var sq = new Square(); | ||
sq.move(-5, -5); | ||
sq.Width = 5; | ||
console.log(sq.area()); | ||
console.log(sq.distance_from_origin()); | ||
|
||
|
||
function Rectangle () { | ||
} | ||
|
||
Rectangle.prototype = new Square(); | ||
Rectangle.prototype.__proto__ = Square.prototype; | ||
Rectangle.prototype.Height = 0; | ||
|
||
Rectangle.prototype.area = function () { | ||
return this.Width * this.Height; | ||
} | ||
|
||
|
||
var re = new Rectangle(); | ||
re.move(25, 25); | ||
re.Width = 10; | ||
re.Height = 5; | ||
console.log(re.area()); | ||
console.log(re.distance_from_origin()); | ||
|
||
|
||
console.log(typeof s); | ||
console.log(typeof sq); | ||
console.log(typeof re); | ||
|
||
console.log(sq instanceof Square); | ||
console.log(sq instanceof Shape); | ||
console.log(sq instanceof Rectangle); | ||
console.log(re instanceof Rectangle); | ||
console.log(sq instanceof Square); | ||
console.log(sq instanceof Shape); | ||
console.log(sq instanceof Date); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
|
||
var car1 = []; | ||
var car2 = new Array(); | ||
var car3 = new Array(10); | ||
var car4 = new Array(4, 34, 6, 8, 525, 8693, 281, 88, 28, 95, 346); | ||
|
||
|
||
// creating | ||
var arr1 = []; | ||
// set values | ||
|
||
for (var i = 0; i < 10; i++) { | ||
arr1[i] = i; | ||
} | ||
|
||
// fills in undefined | ||
arr1.length = 20; | ||
arr1[20] = "new value"; | ||
|
||
console.log(arr1.length); | ||
console.log(arr1[0]); | ||
console.log(arr1); | ||
|
||
|
||
// set values with string index | ||
var arr2 = []; | ||
|
||
arr2["cat"] = "meow"; | ||
arr2["dog"] = "woof"; | ||
|
||
console.log(arr2.length); | ||
console.log(arr2[0]); | ||
console.log(arr2); | ||
|
||
|
||
|
||
// mixed indexes (bad idea) | ||
var arr3 = []; | ||
|
||
arr3[2] = 2; | ||
arr3[3] = 3; | ||
arr3["horse"] = "neigh"; | ||
arr3["狗"] = "王"; | ||
|
||
|
||
console.log(arr3.length); | ||
console.log(arr3[0]); | ||
console.log(arr3); | ||
|
||
|
||
|
||
// multi-dimensional | ||
//var arr4 = [][]; not ok | ||
//var arr5 = [3][3]; // not ok | ||
|
||
// to create a 3x3 | ||
|
||
var tx3A = new Array(new Array(3), new Array(3), new Array(3)); | ||
var tx3B = []; | ||
|
||
for (var i = 0; i < 3; i++) { | ||
tx3B[i] = new Array(3); | ||
} | ||
|
||
|
||
console.log(tx3A); | ||
console.log(tx3B); | ||
|
||
|
||
|
||
// why use arrays when objects contain much of the same functionality: V8 optmises heavily, extra operations slice(), push pop, shift, unshift | ||
|
||
|
||
// key operations push pop | ||
// shift unshift | ||
|
||
|
||
var random = new Array(1, 342, 53, 38, 85958, 3584934, 8459, 2, 69, 1396, 146, 194); | ||
|
||
|
||
// print squares | ||
random.forEach(function (element, index, array) { | ||
console.log(element + "^2 = " + element * element); | ||
}); | ||
|
||
|
||
var squares = random.map(function (element, index, array) { | ||
return element * element; | ||
}); | ||
console.log(squares); | ||
|
||
var evens_only = random.filter(function (element, index, array) { | ||
return (element % 2) == 0; | ||
}); | ||
console.log(evens_only); | ||
|
||
|
||
|
||
console.log(random.join(", ")); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
|
||
|
||
function printit(var_name) { | ||
console.log(global[var_name]); | ||
} | ||
|
||
global.fish = "swordfish"; | ||
global.pet = "cat"; | ||
|
||
printit("fish"); | ||
printit("pet"); | ||
printit("fruit"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
|
||
var obj1 = {}; | ||
var obj2 = new Object(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
|
||
var arr = []; | ||
|
||
arr[0] = 1; | ||
arr[1] = 2; | ||
arr["cat"] = 'meow'; | ||
|
||
console.log(arr.length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
|
||
function uhoh () { | ||
throw new Error("Something bad happened!"); | ||
} | ||
|
||
try { | ||
uhoh(); | ||
} catch (e) { | ||
console.log("I caught an error: " + e.message); | ||
} | ||
|
||
console.log("program is still running"); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
BASIC TYPES: | ||
|
||
|
||
there are several core types: | ||
|
||
numbers, strings, booleans, functions, objects | ||
|
||
null, undefined are actually both types, although just particular instances of objects, and arrays a special case of objects. | ||
|
||
if we have a variable in javascript, type is | ||
|
||
typeof x | ||
|
||
note arrays will return 'object'. see section on arrays. | ||
|
||
|
||
|
||
NUMBERS | ||
|
||
- 64bit double precision floating point numbers -- there are no "integer" types in javascript. | ||
- that means you have 53 bits of precision. | ||
- if you are going to represent 64bits in javascript, use strings or separate library | ||
- if you're doing math ops on floating point numbers, be careful | ||
- 0.1 + 0.2 != 0.3 | ||
- integer numbers < 53 bits will be great because they can be exactly rep'd | ||
- dividing by zero gives you (-)Infinity | ||
- you can convert strings to numbers with parseInt or parseFloat | ||
- if they fail, return NaN | ||
- isNaN | ||
- can test for a "valid" number with isFinite() | ||
|
||
|
||
|
||
BOOLEANS: | ||
|
||
- can have the value true or false | ||
- you can force things to boolean with the Booelan(XXX) function, but rarely necessary | ||
|
||
|
||
STRINGS: | ||
|
||
- strings are sequences of unicode characters | ||
- great for most characters around the world | ||
- no separate character data type -- can just use 1-char strings | ||
- to get the length, jut use .length | ||
var s = "my string"; | ||
console.log(s.length); | ||
// or | ||
console.log("my string".length); | ||
|
||
many interesting functions on strings: | ||
|
||
int str.indexOf("there"); | ||
string "hello there".slice(5, 6) == " " // true | ||
string "hello there".substr(5, 1) == " " // true | ||
array "1,2,3,4,5".split(",") | ||
|
||
|
||
|
||
null is a special value indicates non-value | ||
undefined means no such thing or no value set yet | ||
|
||
var x; | ||
-> undefined | ||
console.log(x); | ||
-> undefined | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
$file = fopen('info.txt', 'r'); | ||
// wait until file is open | ||
|
||
$contents = fread($file, 100000); | ||
// wait until contents are read | ||
|
||
// do something with those contents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
setTimeout(function () { | ||
console.log("I've done my work!"); | ||
}, 2000); | ||
|
||
|
||
console.log("I'm waiting for all my work to finish."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var fs = require('fs'); | ||
|
||
var file; | ||
var buf = new Buffer(100000); | ||
|
||
fs.open( | ||
'info.txt', 'r', | ||
function (err, handle) { | ||
file = handle; | ||
} | ||
); | ||
|
||
fs.read( | ||
file, buf, 0, 100000, null, | ||
function (err, length) { | ||
console.log(buf.toString()); | ||
fs.close(file, function () { /* don't care */ }); | ||
} | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
var fs = require('fs'); | ||
|
||
fs.open( | ||
'info.txt', 'r', | ||
function (err, handle) { | ||
var buf = new Buffer(100000); | ||
fs.read( | ||
handle, buf, 0, 100000, null, | ||
function (err, length) { | ||
console.log(buf.toString('utf8', 0, length)); | ||
fs.close(handle, function () { /* don't care */ }); | ||
} | ||
); | ||
} | ||
); |
Oops, something went wrong.