Variable lift
One of the easiest lifting applications is moving a variable. If we did not have a raise variable, this would throw a ReferenceError :
var bar = foo; var foo;
This does not seem immediately useful, but it allows us to do such things:
var myCoolJS = myCoolJS || {};
This basically means that it looks like this: myCoolJS is myCoolJS if it exists, or a new object if it does not. The second myCoolJS does not throw a ReferenceError if myCoolJS does not exist yet because this variable declaration is raised.
This saves us from having to do an inconvenient check of typeof myCoolJS != 'undefined' .
Lift function
The lift function can be especially useful when combining several scenarios into one. For example, I created an easy implementation of the assembly time of CommonJS modules . This provides the same module , require and exports functions that are in node.js. I built the tool so that the necessary modules consist of several files. For example, require('/foo') can lead to the creation of a module consisting of two files, foo.js ("body file") and foo.h.js ("header file").
This allows the "body file" not to know about the free variables provided by the CommonJS module environment; all this is processed in the header. This makes the code reusable and easily tested without assembly. However, since headers are added to the body, we use the hoisting function in the body file to allow export in headers. For example:
// dom.h.js var util = require('util').util; exports.css = css; // we can do this because "css" is hoisted from below // ... other exports ...
...
// dom.js function css(){}; // this would normally just be an object. css.hasClass = function(element) { ... }; css.addClass = function(element) { ... }; // ...other code...
Dagg Nabbit Mar 05 2018-12-12T00: 00Z
source share