with
Compile time `with` for strict mode JavaScript
Last updated 11 years ago by forbeslindesay .
MIT · Repository · Original npm · Tarball · package.json
$ npm install with 
SYNC missed versions from official npm registry.

with

Compile time with for strict mode JavaScript

Build Status Rolling Versions NPM version

Installation

$ npm install with

Usage

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));'

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'); // => 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).

Parsing Errors

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.

License

MIT

Current Tags

  • 7.0.2-canary-3                                ...           canary (4 years ago)
  • 7.0.2                                ...           latest (4 years ago)

26 Versions

  • 7.0.2                                ...           4 years ago
  • 7.0.2-canary-3                                ...           4 years ago
  • 7.0.1                                ...           4 years ago
  • 7.0.1-canary-2                                ...           4 years ago
  • 7.0.0                                ...           4 years ago
  • 7.0.0-canary-1                                ...           4 years ago
  • 6.0.0                                ...           6 years ago
  • 5.1.1                                ...           8 years ago
  • 5.1.0                                ...           8 years ago
  • 5.0.2                                ...           8 years ago
  • 5.0.1                                ...           8 years ago
  • 5.0.0                                ...           10 years ago
  • 4.0.3                                ...           10 years ago
  • 4.0.2                                ...           10 years ago
  • 4.0.1                                ...           10 years ago
  • 4.0.0                                ...           10 years ago
  • 3.0.1                                ...           10 years ago
  • 3.0.0                                ...           11 years ago
  • 2.0.0                                ...           11 years ago
  • 1.1.1                                ...           11 years ago
  • 1.1.0                                ...           11 years ago
  • 1.0.4                                ...           11 years ago
  • 1.0.3                                ...           11 years ago
  • 1.0.2                                ...           11 years ago
  • 1.0.1                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (1)
Dependents (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE