updated require docs

This commit is contained in:
walterhiggins 2013-12-21 09:10:59 +00:00
parent f25f4ac7f3
commit 4ffc5f6850

View file

@ -272,35 +272,37 @@ See [issue #69][issue69] for more information.
[issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 [issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69
# Require - Node.js-style module loading in ScriptCraft ## Require - Node.js-style module loading in ScriptCraft
### (Experimental as of 2013-12-21) #### (Experimental as of 2013-12-21)
Node.js is a server-side javascript environment with an excellent Node.js is a server-side javascript environment with an excellent
module loading system based on CommonJS. Modules in Node.js are really module loading system based on CommonJS. Modules in Node.js are really
simple. Each module is in its own javascript file and all variables simple. Each module is in its own javascript file and all variables
and functions within the file are private to that file/module only. There is a very concise explanation of CommonJS modules at and functions within the file are private to that file/module only.
http://wiki.commonjs.org/wiki/Modules/1.1.1. There is a very concise explanation of CommonJS modules at...
[http://wiki.commonjs.org/wiki/Modules/1.1.1.][cjsmodules]
If you want to export a variable or function you use the module.export If you want to export a variable or function you use the module.export
property. property.
For example imagine you have 3 files program.js, inc.js and math.js ... For example imagine you have 3 files program.js, inc.js and math.js ...
## math.js ### math.js
exports.add = function(a,b){ exports.add = function(a,b){
return a + b; return a + b;
} }
## inc.js ### inc.js
var math = require('./math'); var math = require('./math');
exports.increment = function(n){ exports.increment = function(n){
return math.add(n, 1); return math.add(n, 1);
} }
## program.js ### program.js
var inc = require('./inc').increment; var inc = require('./inc').increment;
var a = 7; var a = 7;
@ -319,6 +321,12 @@ support node modules. Node.js and Rhino are two very different
Javascript environments. ScriptCraft uses Rhino Javascript, not Javascript environments. ScriptCraft uses Rhino Javascript, not
Node.js. Node.js.
Right now, the base directory is for relative modules is 'js-plugins'.
Modules can be loaded using relative or absolute paths. Per the CommonJS
module specification, the '.js' suffix is optional.
[cjsmodules]: http://wiki.commonjs.org/wiki/Modules/1.1.1.
Drone Module Drone Module
============ ============
The Drone is a convenience class for building. It can be used for... The Drone is a convenience class for building. It can be used for...