Wednesday, January 25, 2012

Simple Intro to NodeJS Module Scope

People often ask about scope and visibility in Node.JS modules. What's visible outside the module versus local to the module? How can you write a module that automatically exports an object? And so on.

Here are six examples showing six ways of defining things in Node.JS modules.  Not all of these are recommended (in fact, a couple of them are definitely NOT recommended). But these simple examples should help explain how modules and their exports work, intuitively.

foo.js

This module defines a global function called foo. By not putting var in front of the declaration of foo, foo is global. This is really not recommended. You should avoid polluting the global namespace.

// foo.js
foo = function () {
  console.log("foo!");
}

Here is an app that use it.

// Module supplies global function foo()
require('./foo');
foo();

bar.js

This module exports an anonymous function.

// bar.js
module.exports = function() {
  console.log("bar!");
}

Here is an app that use it.

// Module exports anonymous function
var bar = require('./bar');
bar();

fiz.js

This exports a function called fiz.

// fiz.js
exports.fiz = function() {
  console.log("fiz!");
}

Here is an app that use it.

// Module exports function fiz()
var fiz = require('./fiz').fiz;
fiz();

buz.js

This exports an anonymous object.

// buz.js
var Buz = function() {}; 

Buz.prototype.log = function () {
  console.log("buz!");
};
module.exports = new Buz();

Here is an app that use it.

// Module exports anonymous object
var buz = require('./buz');
buz.log();

baz.js

This exports an object called Baz.

// baz.js
var Baz = function() {}; 

Baz.prototype.log = function () {
  console.log("baz!");
};

exports.Baz = new Baz();

Here is an app that use it.

// Module exports object Baz
var baz = require('./baz').Baz;
baz.log();

qux.js

This exports a prototype called Qux.

// qux.js
var Qux = function() {}; 
Qux.prototype.log = function () {
  console.log("qux!");
};
exports.Qux = Qux;

Here is an app that use it.

// Module exports a prototype Qux
var Qux = require('./qux').Qux;
var qux = new Qux();
qux.log();

Thursday, January 5, 2012

The Global Power Shift

Paddy Ashdown's fantastic speech at TEDx Brussels, which was published today.  This is well worth watching for anyone interested in what is happening in global power structures and the end of the American century.  Absolutely brilliant talk, at once troubling and still inspirational.

Saturday, December 31, 2011

The Battle Path

Writing-On-Stone Provincial Park, Alberta, Canada

© 2012 Darren DeRidder

Monday, December 12, 2011

Thursday, December 8, 2011

Checking In

Hard to believe the month of November flew by without so much as a peep out of 51 Elliot.  Between getting my boat put away for the winter, removing stain from my porch and cleaning the wood, spending too many hours on floorplanner.com and various architectural websites, and raking up about 87 bags of autumn leaves, it just happened.

And, instead of posting my usual techo / philosophical points of view, the last couple of months have been mainly a time to sit back and observe. The Occupy movement has been in the news a lot, the OpenMedia.ca campaign in Canada has been building momentum, and businesses of the digital economy are speaking out against the abuse of copyright by media conglomerates as manifest in the much-hated SOPA and Protect-IP proposals. I'm looking forward to seeing how it all plays out, and I hope it results in a better world for everyone.  A Christmas wish, a bit early.

I did manage to accumulate a fairly big back log of interesting articles, so here to make up for my recent silence is the second ever web-dump of nifty things I found interesting:

US Big Brother wholesale spying aka. the PATRIOT Act is taking a bite out of the US cloud computing market. Business are choosing not to use the products and services of companies like Microsoft and Amazon, who are bound by the laws of a government that has created what is considered an "indemic surveillance society".

Fabian Pascal has a very good paper on database normalization, integrity and performance.

The first in a series of posts on damn cool algorithms... this one's on BK Trees.

This was the year for Javascript. Javascript on the client, Javascript on the server, everywhere you look Javascript. This is great! Why? Because JavaScript Is Not Java.  Thank goodness.

This year Node.js, the server-side Javascript engine based on Google's blazing-fast V8 Javascript engine, took off like wildfire. Node.js modules you should know about is a series of posts on... um, Node.js modules you should know about.

I know that Chrome is what all the cool kids are using. But, given the aforementioned reference to Big Brother, I still use Firefox frequently.  For web developers, there are a ton of great web developer plugins for Firefox, and this post covers a half-gazillion of them.

Paper.js is a neat little Javascript graphics library for programmers of neat little Javascript graphics.

Or perhaps you want to try doing face detection in Javascript?

One of my biggest complaints about any library-heavy language is poor curation. Java suffers terribly from this, and so do jQuery and Node.js to a degree. That's why I thought The Hand-picked jQuery Plugins Repo was pretty interesting.

Mozilla Popcorn hasn't been generating the same amount of buzz as some of the other web debutantes, but it looks interesting for the future of web video. In particular the National Film Board's project "One Millionth Tower" is... really cool to watch and play with.

It was about a year ago I predicted a rise in popularity of darknets or mesh networks. So I was delighted to find a solitary article detailing the rise in popularity of darknets. Well, there were a couple, actually.  This is going to happen sooner or later because politicians and corporate fascists are too technologically illiterate to understand the way their old-school strategies are pushing new-school innovation forward. As a colleague of mine says, the Internet views censorship as a failure and routes around it.

The JavaScript PC Emulator by Fabrice Bellard is mind-numbingly jaw-droppingly cool. If you're into that sorta thing. Bellard is one amazing researcher.

I really like the Crescent Road House. It's beautiful.

We have it pretty good in Canada. Canadians are officially richer than Americans. We have better privacy laws. We have a great health care system.  But, what keeps high tech innovation from happening here on a larger scale. According to this post, it might be because Canadian laws would have made starting up Facebook or Youtube impossible in Canada.  This is one for the conservatives to pay attention to, with their pandering to the US Copyright-abuse industry.

Do you feel like taking a course in Machine Learning from Stanford University? Of course you do.  It's free. As should all education be in this information age. And will be.  Oops, I guess that's my prediction for this post.  Watch for a movement around free higher education based on meritocratic principles.

Oh, here's that other article on the rise of darknets / mesh networks that I was talking about.  Why are people doing this? Fear of repression.  Land of the free, home of my ass.

How many mobile frameworks could a woodchuck chuck, if a woodchuck could chuck mobile frameworks? He'd chuck all the mobile frameworks on this page and then probably end up using Phonegap.

I can't resist one last JavaScript item, a truly incredible and beautiful work by Mr. Doob... yep, it's three.js. I used to say that web technology was poised to do everything traditional desktop software programming environments could do, with the exception of 3D video games.  That is no longer the case.  I for one, welcome our new JavaScript overlords!

Happy reading and have a wonderful week.

Tuesday, October 11, 2011

Pink Lake

Pink Lake, Gatineau Park, Quebec

 © 2011 by Darren DeRidder

Wednesday, September 28, 2011