17. January 2015
HAXE and NodeJS
Continuing the sudy of javascript target of HAXE we will focus on NodeJS this time. Before we move on, lets remind ourselves the things to expect from the HAXE toolchain by going through the previous two introductions, here is the first and the second post.
Thats about HAXE, now a little about NodeJS. In NodeJS the concept of modules is very common to make some re-usable code. In general, modules hide everything by default. In class based languages we can call them as private
elements. To make something public
, we have to use exports
keyword in NodeJS.
For example, if we have a file names one.js
as
exports.publicFunction = function() {
console.log('Hello World');
}
Now, in NodeJS, we can write something as
var oneObj = require('./one');
oneObj.publicFunction();
Thats first kind of modules we have. There is another kind of modules in NodeJS, where instead of exposing each individual elements from the modules, it exposes a Constructor
function. This is done with module.exports
in stead of only exports
.
module.exports.MyObjConstructor = function() {
console.log('New Object');
}
This second one is looked at as classical concepts of programming, where each JS file is kind of a Class file.
The dynamic and fun nature of javascript allows us to use the first way of exposing individual elements to actually expose a constructor function too. Is not it ?! Why we think about that! Because HAXE generated javascript is actually doing that. Smart.