Coder Social home page Coder Social logo

with's Introduction

with

Compile time with for strict mode JavaScript

build status Dependency Status 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).

License

MIT

with's People

Contributors

forbeslindesay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.