with
Compile time with
for strict mode JavaScript
Installation
$ npm install with
Usage
var addWith = require('with')
addWith('obj', 'console.log(a)')
addWith('obj', 'console.log(a)', ['console'])
API
addWith(obj, src[, exclude])
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')
var obj = {foo: 'foo'}
with ({}) {
foo = 'bar'
}
assert(obj.foo === 'bar')
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).
License
MIT