Modules
Fancy word for a file/folder that contains code.
Node Modules
arguments
keyword:
function func() { console.log(arguments) }
func(3, 4, 5, 6)
Node is not only executing a line of code, it actually wraps the file with a function:
console.log(arguments); // suppose this is at top level of the file
it's actually like this:
function _(exports, module, require, __filename, __dirname) {
console.log(arguments);
return module.exports;
}
We usually use exports
keyword to export things, it's actually not a global keyword or something, it's just the first argument to the hidden wrapping function. Also, variables declared at top level are not global as well.
Browser don't like Node, it doesn't have hidden wrapping function, so variables declared at top level is global.
exports
is just alias of module.exports
.
(exports, module, ...) { // Node does this wrap (module.exports, module, ...)
exports.a = 1
module.exports.b = 99
// if we do like this, we're not changing the module object, we just assign new pointer that no longer point to module.exports
exports = ...
exports = () => {} // break the assignment reference
module.exports = () => {} // ok because module.exports is actually what is being returned
}
Types of API objects
Object
Top-level API is a simple object. So, no need to use module.exports
exports.name = 'Tu'
exports.age = 23
const me = require('./me')
console.log(me.name, me.age)
Array, String, Function
Use module.exports
.
module.exports = [2, 3, 4, 7]
Global Object
setTimeout() // implies global.setTimeout()
You can actually make object globally by attaching it to global
.
Avoid doing that!
Errors & Exceptions
Error is "problem", exception is "condition".