From 3d2e30d764cdf5f89ce743b6f2f8c2f3f659b2ff Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Sun, 15 Dec 2019 09:20:01 +0200 Subject: DOCS: js: minor update for modules/require --- DOCS/man/javascript.rst | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'DOCS') diff --git a/DOCS/man/javascript.rst b/DOCS/man/javascript.rst index 8399c77d6a..0c29affcdf 100644 --- a/DOCS/man/javascript.rst +++ b/DOCS/man/javascript.rst @@ -278,9 +278,11 @@ CommonJS modules and ``require(id)`` ------------------------------------ CommonJS Modules are a standard system where scripts can export common functions -for use by other scripts. A module is a script which adds properties (functions, -etc) to its invisible ``exports`` object, which another script can access by -loading it with ``require(module-id)`` - which returns that ``exports`` object. +for use by other scripts. Specifically, a module is a script which adds +properties (functions, etc) to its pre-existing ``exports`` object, which +another script can access with ``require(module-id)``. This runs the module and +returns its ``exports`` object. Further calls to ``require`` for the same module +will return its cached ``exports`` object without running the module again. Modules and ``require`` are supported, standard compliant, and generally similar to node.js. However, most node.js modules won't run due to missing modules such @@ -293,10 +295,11 @@ will load the file ``./foo.js`` and return its ``exports`` object. An id is relative (to the script which ``require``'d it) if it starts with ``./`` or ``../``. Otherwise, it's considered a "top-level id" (CommonJS term). -Top level id is evaluated as absolute filesystem path if possible (e.g. ``/x/y`` -or ``~/x``). Otherwise, it's searched at ``scripts/modules.js/`` in mpv config -dirs - in normal config search order. E.g. ``require("x")`` is searched as file -``x.js`` at those dirs, and id ``foo/x`` is searched as file ``foo/x.js``. +Top level id is evaluated as absolute filesystem path if possible, e.g. ``/x/y`` +or ``~/x``. Otherwise, it's considered a global module id and searched at +``scripts/modules.js/`` in mpv config dirs - in normal config search order. E.g. +``require("x")`` is searched as file ``x.js`` at those dirs, and id ``foo/x`` is +searched as file ``x.js`` inside dir ``foo`` at those dirs. No ``global`` variable, but a module's ``this`` at its top lexical scope is the global object - also in strict mode. If you have a module which needs ``global`` -- cgit v1.2.3