var func = function(arg) {
var kept = arg + 6;
return function() {
return kept;
};
};
var getter = func(7);
getter() === 13;
... which is basically a hash
var func = function() {
console.log("Oooh, space! Spaaace!");
};
var hash = {"Eggs": "green"};
var array = [1, 2, 3];
(func instanceof Object) === true;
(hash instanceof Object) === true;
(array instanceof Object) === true;
Alas, no method_missing-like functionality, however; objects aren't message receivers
var fluffy = {
vocalization: "meow",
giveMeat: function() {
return this.vocalization;
}
};
fluffy.giveMeat() === "meow";
fluffy["giveMeat"]() === undefined;
var Penguin = function(name) {
this.giveFish = function() {
// your filesystems are safe, this time.
return name + " says, beep";
};
};
var tux = new Penguin("tux");
console.log(tux.giveFish());
(By necessity!)
With a decent number of batteries included, especially for networking!
Implemented with libev
Implemented with libeio
git clone git://github.com/joyent/node.git
NB: It's still moving fast, so distro packages aren't useful yet
curl -s http://npmjs.org/install.sh | sudo sh
$ ls
lib/ package.json runspecs.js* myapp.js*
{
"name": "myapp",
"description": "Achieve nothing short of world domination",
"version": "0.1.0",
"homepage": "https://github.com/orospakr/myapp",
"repository": "git://github.com/orospakr/myapp.git",
"author": "Andrew Clunis <[email protected]>",
"directories": {
"lib": "lib"
},
"dependencies": {
"warpdrive": "1.0.0rc1"
},
"engines": {
"node": "*"
},
"scripts": {
"test": "runspecs.js",
"run": "myapp.js"
},
}
#!/usr/bin/env node
console.log("Welcome to your first node.js app!");
#!/usr/bin/env node
# mostly stolen from nodejs.org!
var net = require('net');
var server = net.createServer(function (socket) {
socket.write("Echo server\r\n");
socket.on("data", function(data) {
console.log("Got '" + data + "'");
});
socket.on("error", function(e) {
console.log("Yikes, hard error: " + e.message);
});
socket.on("close", function() {
console.log("Goodbye!");
});
socket.pipe(socket);
});
server.listen(61234, "0.0.0.0");
#!/usr/bin/env node
var http = require('http');
var util = require('util');
var HTTPHandler = function() {
this.counter = 0;
this.consumeRequest = function(req, response) {
response.writeHead(200, {"Content-Type": "text/html"});
// if you set the Content-Length field manually, chunked mode is disabled
response.end("<html><body><h1>Req #" + ++this.counter + "</h1></body></head>");
console.log("Request answered: " + util.inspect(req));
};
};
var handler = new HTTPHandler();
var server = http.createServer(handler.consumeRequest.bind(handler));
server.listen(8081, "::");