first=class functions - in js we can treat function as anyother type like assign something value/function, put in array
joyent -> node.js -> v8 -> cpp
function statment function greet() { console.log('hiiii !'); } greet() // hiiii !
// 1. functions are first-class in js
function callFunc(fn) { fn(); } callFunc(greet); // hiiii !
// 2. function expression
var greetMe2 = function () { console.log('Hi from expresssion !! '); } greetMe2(); callFunc(greetMe2); //Hi from expresssion !!
//3. function express on the fly
callFunc(function(){ console.log('hiiii !') });
NODE MODULE 1.
app.js greet.js app.js require('./greet.js') greet.js function greet() { console.log('hiiii !'); } greet()
note: greet function in greet.js will not collid with greet function in app.js else or the greet function will not be avaiable in app.js directly to call like greet(); its a very good feature of nodejs like modularized !
RUN node app.js
if you want to make greet() function from greet.js avaiable in app.js here is what we need to do
app.js var greet = require('./greet') greet(); greet.js var greet = function () { console.log('Hello!'); } module.exports = greet;
HOW DOES MODULES IN NODE WORKS ?????
when we use require node method what actually happens ??? this is the reason why the function and variable in required files are not available in global scope.
var greet = require('./greet.js');
1- wraps the containt of the file in
(function(exports, require, module, __filenmae, __dirname) { });
2- run the code using . apply(params...)
fn(module.exports, require, module, filename, dirname);
3- return the module.exports;
return module.exports;
Note: when we requires a file in nodejs and that file does not exist nodejs will look for the folder with same name and pickup index.js in that folder.
do exmaple
// Greet 01
module.exports = function() { console.log('Hello world'); };
// Greet 02
module.exports.greet = function() { console.log('Hello world!'); };
// Greet 03
function Greetr() { this.greeting = 'Hello world!!'; this.greet = function() { console.log(this.greeting); } } module.exports = new Greetr();
// Greet 04
function Greetr() { this.greeting = 'Hello world!!!'; this.greet = function() { console.log(this.greeting); } } module.exports = Greetr;
// Greet 05
var greeting = 'Hello world!!!!'; function greet() { console.log(greeting); } module.exports = { greet: greet }
app.js
var greet = require('./greet1'); greet(); var greet2 = require('./greet2').greet; greet2(); var greet3 = require('./greet3'); greet3.greet(); greet3.greeting = 'Changed hello world!'; var greet3b = require('./greet3'); greet3b.greet(); var Greet4 = require('./greet4'); var grtr = new Greet4(); grtr.greet(); var greet5 = require('./greet5').greet; greet5();
UTIL INHERITS
Copy prototype of Super to Sub using util.inherits
var EventEmitter = require('events'); var util = require('util'); function Greetr() { // 2. Line below is the way to add all properties and methods which are added to constructor function and not added via prototype // like a super call EventEmitter.call(this); this.greeting = 'Hello world!'; } //1. Following lines copies all prototype properties and methods from EventEmitter to Greetr, in another works Greetr no inherites EventEmitter util.inherits(Greetr, EventEmitter); Greetr.prototype.greet = function(data) { console.log(this.greeting + ': ' + data); this.emit('greet', data); } var greeter1 = new Greetr(); greeter1.on('greet', function(data) { console.log('Someone greeted!: ' + data); }); greeter1.greet('Tony');
Node does things asynchronous V8 does not. Javascript is synchronous
All node asynchronous code hadled my libuv for more info visit libuv.org
try nodemon