JavaScript Scope: Closure
Closures provide a way to hold or trap an activation object in memory.
They can be used for defining private variables, and storing variables outside of a functions life-cycle.
JavaScript Scope: What is this?
‘this’ will always resolve to the global object unless:
The containing function is instantiated with the ‘new’ keyword
var me = this;
function Test(){
console.log(this === me);
}
var myTest = new Test(); // false
Or.. The function is called on an owner object.
var obj = {
myFunc: function(){
return this;
}
};
console.log( this === obj.myFunc() ); // false
JavaScript Scope: Execution Context
The Execution Context is an abstract representation of the current runtime environment in JavaScript. To explain a little better, the Execution Context is formed of three components.
- Lexical Environment
- Variable Environment
- This binding
Free no.de domain for node.js server
At the time of writing this, Joyent are literally giving away free ‘smartmachines’ for running nodejs servers with no.de domains.
Awesome, I got one by going to their site, creating an account, and then running the curl command they gave me to request a coupon. I think they make you do the last step to make sure you are actually a geek… A week later I got an email explaining how to use their API to get the coupon. I logged in, and provisioned the machine.
Deployment is easy with git, and they provide a good Get started guide. I had some problems because the version of Node I had locally was older than the one installed by default on the Joyent sever. You can define which version you want to use with a config file, though best stick to the latest version to avoid versioning issues with other node modules like Express.js. They have npm installed as well so it is really easy to install he modules you want.
#WIN
JavaScript BDD with Jasmine and JsTestDriver
You may have heard about Jasmine, it’s a JS framework for supporting BDD. It has become very popular so for the last couple of months I have been playing with it to see if it could work for us.
The big hurdle for integration at my company was that we are already tied to another JS test framework, JsTestDriver or JSTD for short.
Keep it simple
It is very easy to overcomplicate things, whether in life or work it is always best to stand back and take some time to think about the problem, understand what needs to be solved. Now you could methodically go through through every possible solution, weigh up the pros and cons and then decide what to do, that’s fine, that works. But I think it is easy to miss things, an edge case or a related problem.
The best thing to do is literally walk away from it, if you have the luxury of time then use that time to talk to others, research. Or even better, pick up a guitar, draw a pretty picture; do something else.
Node.js fun
I’ve started to play around with node.js (the SSJS framework built on V8). I had a bit of trouble installing it on my old version of Ubuntu so opted to install it on Cygwin. If you are planning to do the same I’d recommend you read Getting Started with Node.js on Windows and Node.js on Windows 7 under Cygwin, “FixImage error 13″ problem.
I very quickly built a hello world app, it was really easy. Great I thought, what next … A real time chat app?
A few hours later I’d built just that, albeit mostly using the Socket.IO chat example. Socket.IO is ridiculously powerful, it enables push messaging from the server to the client and apparently scales really well.
So then I wanted to build multiple pages, multiple chatrooms. Enter Express.js. Express.js makes URI routing, session handling and view rendering so much easier. It’s easy to use as well.
Next.. Chat rooms are boring, lets add some HTML5 in the form of Canvas. Taking a simple jQuery canvas plugin called Scratch Pad and hooking it up to Socket.IO I was able to push custom events like ‘startDraw’ down to the server whereupon the server broadcasts that event to all the listening clients, the client then updates the canvas.
By the following weekend I had built a real time chat application (and I mean real time) which also enables collaborative drawing.
I’m am really loving Node.js at the moment.