GrauKatze 05f70669c9 Сконфигурирована и подготовлена база для проекта | před 2 roky | |
---|---|---|
.. | ||
.github | před 2 roky | |
lib | před 2 roky | |
node_modules | před 2 roky | |
src | před 2 roky | |
.babelrc | před 2 roky | |
LICENSE | před 2 roky | |
README.md | před 2 roky | |
package.json | před 2 roky | |
prettier.config.js | před 2 roky | |
tsconfig.json | před 2 roky | |
tslint.json | před 2 roky |
Compile time with
for strict mode JavaScript
$ npm install with
var addWith = require('with');
addWith('obj', 'console.log(a)');
// => ';(function (console, a) {
// console.log(a)
// }("console" in obj ? obj.console :
// typeof console!=="undefined" ? console : undefined,
// "a" in obj ? obj.a :
// typeof a !== "undefined" ? a : undefined));'
addWith('obj', 'console.log(a)', ['console']);
// => ';(function (console, a) {
// console.log(a)
// }("a" in obj ? obj.a :
// typeof a !== "undefined" ? a : undefined));'
The idea is that this is roughly equivallent to:
with (obj) {
src;
}
There are a few differences though. For starters, assignments to variables will always remain contained within the with block.
e.g.
var foo = 'foo';
with ({}) {
foo = 'bar';
}
assert(foo === 'bar'); // => This fails for compile time with but passes for native with
var obj = {foo: 'foo'};
with ({}) {
foo = 'bar';
}
assert(obj.foo === 'bar'); // => This fails for compile time with but passes for native with
It also makes everything be declared, so you can always do:
if (foo === undefined)
instead of
if (typeof foo === 'undefined')
This is not the case if foo is in exclude
. If a variable is excluded, we ignore it entirely. This is useful if you know a variable will be global as it can lead to efficiency improvements.
It is also safe to use in strict mode (unlike with
) and it minifies properly (with
disables virtually all minification).
with internally uses babylon to parse code passed to addWith
. If babylon throws an error, probably due to a syntax error, addWith
returns an error wrapping the babylon error, so you can
retrieve location information. error.component
is "src"
if the error is in the body or "obj"
if it's in the object part of the with expression. error.babylonError
is
the error thrown from babylon.
MIT