~ubuntu-branches/ubuntu/vivid/nodejs/vivid

« back to all changes in this revision

Viewing changes to doc/api/modules.json

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-11-13 23:17:51 UTC
  • mfrom: (1.1.29)
  • mto: This revision was merged to the branch mainline in revision 33.
  • Revision ID: package-import@ubuntu.com-20131113231751-m6uqywp5dc4s4fxo
Tags: 0.10.22~dfsg1-1
* Upstream update. 
* Refresh patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
      "name": "module",
7
7
      "stability": 5,
8
8
      "stabilityText": "Locked",
9
 
      "desc": "<p>Node has a simple module loading system.  In Node, files and modules are in\none-to-one correspondence.  As an example, <code>foo.js</code> loads the module\n<code>circle.js</code> in the same directory.\n\n</p>\n<p>The contents of <code>foo.js</code>:\n\n</p>\n<pre><code>var circle = require(&#39;./circle.js&#39;);\nconsole.log( &#39;The area of a circle of radius 4 is &#39;\n           + circle.area(4));</code></pre>\n<p>The contents of <code>circle.js</code>:\n\n</p>\n<pre><code>var PI = Math.PI;\n\nexports.area = function (r) {\n  return PI * r * r;\n};\n\nexports.circumference = function (r) {\n  return 2 * PI * r;\n};</code></pre>\n<p>The module <code>circle.js</code> has exported the functions <code>area()</code> and\n<code>circumference()</code>.  To export an object, add to the special <code>exports</code>\nobject.\n\n</p>\n<p>Note that <code>exports</code> is a reference to <code>module.exports</code> making it suitable\nfor augmentation only. If you are exporting a single item such as a\nconstructor you will want to use <code>module.exports</code> directly instead.\n\n</p>\n<pre><code>function MyConstructor (opts) {\n  //...\n}\n\n// BROKEN: Does not modify exports\nexports = MyConstructor;\n\n// exports the constructor properly\nmodule.exports = MyConstructor;</code></pre>\n<p>Variables\nlocal to the module will be private. In this example the variable <code>PI</code> is\nprivate to <code>circle.js</code>.\n\n</p>\n<p>The module system is implemented in the <code>require(&quot;module&quot;)</code> module.\n\n</p>\n",
 
9
      "desc": "<p>Node has a simple module loading system.  In Node, files and modules are in\none-to-one correspondence.  As an example, <code>foo.js</code> loads the module\n<code>circle.js</code> in the same directory.\n\n</p>\n<p>The contents of <code>foo.js</code>:\n\n</p>\n<pre><code>var circle = require(&#39;./circle.js&#39;);\nconsole.log( &#39;The area of a circle of radius 4 is &#39;\n           + circle.area(4));</code></pre>\n<p>The contents of <code>circle.js</code>:\n\n</p>\n<pre><code>var PI = Math.PI;\n\nexports.area = function (r) {\n  return PI * r * r;\n};\n\nexports.circumference = function (r) {\n  return 2 * PI * r;\n};</code></pre>\n<p>The module <code>circle.js</code> has exported the functions <code>area()</code> and\n<code>circumference()</code>.  To add functions and objects to the root of your module,\nyou can add them to the special <code>exports</code> object.\n\n</p>\n<p>Variables local to the module will be private, as though the module was wrapped\nin a function. In this example the variable <code>PI</code> is private to <code>circle.js</code>.\n\n</p>\n<p>If you want the root of your module&#39;s export to be a function (such as a\nconstructor) or if you want to export a complete object in one assignment\ninstead of building it one property at a time, assign it to <code>module.exports</code>\ninstead of <code>exports</code>.\n\n</p>\n<p>Below, <code>bar.js</code> makes use of the <code>square</code> module, which exports a constructor:\n\n</p>\n<pre><code>var square = require(&#39;./square.js&#39;);\nvar mySquare = square(2);\nconsole.log(&#39;The area of my square is &#39; + mySquare.area());</code></pre>\n<p>The <code>square</code> module is defined in <code>square.js</code>:\n\n</p>\n<pre><code>// assigning to exports will not modify module, must use module.exports\nmodule.exports = function(width) {\n  return {\n    area: function() {\n      return width * width;\n    }\n  };\n}</code></pre>\n<p>The module system is implemented in the <code>require(&quot;module&quot;)</code> module.\n\n</p>\n",
10
10
      "miscs": [
11
11
        {
12
12
          "textRaw": "Cycles",
82
82
          "textRaw": "The `module` Object",
83
83
          "name": "module",
84
84
          "type": "var",
85
 
          "desc": "<p>In each module, the <code>module</code> free variable is a reference to the object\nrepresenting the current module.  In particular\n<code>module.exports</code> is accessible via the <code>exports</code> module-global.\n<code>module</code> isn&#39;t actually a global but rather local to each module.\n\n</p>\n",
 
85
          "desc": "<p>In each module, the <code>module</code> free variable is a reference to the object\nrepresenting the current module.  For convenience, <code>module.exports</code> is\nalso accessible via the <code>exports</code> module-global. <code>module</code> isn&#39;t actually\na global but rather local to each module.\n\n</p>\n",
86
86
          "properties": [
87
87
            {
88
88
              "textRaw": "`exports` {Object} ",
89
89
              "name": "exports",
90
 
              "desc": "<p>The <code>module.exports</code> object is created by the Module system. Sometimes this is not\nacceptable, many want their module to be an instance of some class. To do this\nassign the desired export object to <code>module.exports</code>. For example suppose we\nwere making a module called <code>a.js</code>\n\n</p>\n<pre><code>var EventEmitter = require(&#39;events&#39;).EventEmitter;\n\nmodule.exports = new EventEmitter();\n\n// Do some work, and after some time emit\n// the &#39;ready&#39; event from the module itself.\nsetTimeout(function() {\n  module.exports.emit(&#39;ready&#39;);\n}, 1000);</code></pre>\n<p>Then in another file we could do\n\n</p>\n<pre><code>var a = require(&#39;./a&#39;);\na.on(&#39;ready&#39;, function() {\n  console.log(&#39;module a is ready&#39;);\n});</code></pre>\n<p>Note that assignment to <code>module.exports</code> must be done immediately. It cannot be\ndone in any callbacks.  This does not work:\n\n</p>\n<p>x.js:\n\n</p>\n<pre><code>setTimeout(function() {\n  module.exports = { a: &quot;hello&quot; };\n}, 0);</code></pre>\n<p>y.js:\n\n</p>\n<pre><code>var x = require(&#39;./x&#39;);\nconsole.log(x.a);</code></pre>\n"
 
90
              "desc": "<p>The <code>module.exports</code> object is created by the Module system. Sometimes this is not\nacceptable; many want their module to be an instance of some class. To do this\nassign the desired export object to <code>module.exports</code>. Note that assigning the\ndesired object to <code>exports</code> will simply rebind the local <code>exports</code> variable,\nwhich is probably not what you want to do.\n\n</p>\n<p>For example suppose we were making a module called <code>a.js</code>\n\n</p>\n<pre><code>var EventEmitter = require(&#39;events&#39;).EventEmitter;\n\nmodule.exports = new EventEmitter();\n\n// Do some work, and after some time emit\n// the &#39;ready&#39; event from the module itself.\nsetTimeout(function() {\n  module.exports.emit(&#39;ready&#39;);\n}, 1000);</code></pre>\n<p>Then in another file we could do\n\n</p>\n<pre><code>var a = require(&#39;./a&#39;);\na.on(&#39;ready&#39;, function() {\n  console.log(&#39;module a is ready&#39;);\n});</code></pre>\n<p>Note that assignment to <code>module.exports</code> must be done immediately. It cannot be\ndone in any callbacks.  This does not work:\n\n</p>\n<p>x.js:\n\n</p>\n<pre><code>setTimeout(function() {\n  module.exports = { a: &quot;hello&quot; };\n}, 0);</code></pre>\n<p>y.js:\n\n</p>\n<pre><code>var x = require(&#39;./x&#39;);\nconsole.log(x.a);</code></pre>\n",
 
91
              "modules": [
 
92
                {
 
93
                  "textRaw": "exports alias",
 
94
                  "name": "exports_alias",
 
95
                  "desc": "<p>The <code>exports</code> variable that is available within a module starts as a reference\nto <code>module.exports</code>. As with any variable, if you assign a new value to it, it\nis no longer bound to the previous value.\n\n</p>\n<p>To illustrate the behaviour, imagine this hypothetical implementation of\n<code>require()</code>:\n\n</p>\n<pre><code>function require(...) {\n  // ...\n  function (module, exports) {\n    // Your module code here\n    exports = some_func;        // re-assigns exports, exports is no longer\n                                // a shortcut, and nothing is exported.\n    module.exports = some_func; // makes your module export 0\n  } (module, module.exports);\n  return module;\n}</code></pre>\n<p>As a guideline, if the relationship between <code>exports</code> and <code>module.exports</code>\nseems like magic to you, ignore <code>exports</code> and only use <code>module.exports</code>.\n\n</p>\n",
 
96
                  "type": "module",
 
97
                  "displayName": "exports alias"
 
98
                }
 
99
              ]
91
100
            },
92
101
            {
93
102
              "textRaw": "`id` {String} ",