William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

NodeJS: module.exports

JavaScript Classes, Prototypes, and Closures

Here is a helpful video regarding on NodeJS – how to write the JS codes:
http://nodetuts.com/tutorials/28-javascript-classes-prototypes-and-closures.html#video.
I collected the sample codes and list here for quick retrieving:

var Adder = function(a, b) {
	this.a = a;
	this.b = b;
};
Adder.prototype.add = function() {
	return this.a + this.b;
};
module.exports = Adder;
--> node
> Var Adder = require('./adder');
> Adder [Function]
> var adder = new Adder(1,2);
> adder {object}
> adder.__proto__ { add: [Function] }

---------- 2. Add default --------
var Adder = function(a, b) {
	this.a = a;
	this.b = b;
};
Adder.prototype.coalesce = function() {
	if(! this.a) { this.a = 0; }
	if(! this.b) { this.b = 0; }
};
Adder.prototype.add = funciton() {
	this.coalesce();
	return this.a + this.b;
}
module.exports = Adder;

--> node
> var Adder = require('./adder');
> var adder = new Adder(1);
> adder.add()
> adder.__proto__.coalesce = funciton() {}
> adder.add()
> adder.a
------ 3. Avoid expose: closure -----
var Adder = function(a, b) {
	function coalesce() {
		if(! a) { a = 0; }
		if(! b) { b = 0; }
	};
	this.add = function() {
		coalesce();
		return a + b;
	}
};
module.exports = Adder;
--> node
> var Adder= require('./adder');
> var adder = new Adder(1,2);
> adder
> adder.add();

----- 4. Remove new: return {} ------
var Adder = function(a, b) {
	function coalesce() {
		if(! a) { a = 0; }
		if(! b) { b = 0; }
	};
	function add() {
		coalesce();
		return a + b;
	}
	return {
		add: add
	};
};
module.exports = Adder;
--> node
> var Adder= require('./adder');
> var adder = Adder(1,2);
> adder.__proto__
> adder.add()

----- 5. working with Node's modules as an event emitter ------
var EventEmitter = require('events').EventEmitter;
var Adder = function(a, b) {
	var that;
	function coalesce() {
		if(! a) { a = 0; }
		if(! b) { b = 0; }
	};
	function add() {
		coalesce();
		that.emit('add', a, b);
		return a + b;
	}
	that {
		add: add
	};
	that.__proto__ = EventEmitter.prototype;
	return that;
};
-->node
> var Adder = require('./adder');
> var adder = Adder(1,2)
> adder.on('add', function(a,b) {
	console.log('ADDING', a, 'and', b);
});
> adder.add();
Advertisements

One response to “NodeJS: module.exports

  1. http://www.yukaphotoart.com/ 07/16/2013 at 9:36 am

    I must thank you for the efforts you have put in writing this blog.
    I really hope to check out the same high-grade blog posts
    from you later on as well. In truth, your creative writing abilities has inspired me to get my
    own site now 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: