Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding in context with with #344

Closed
weepy opened this issue Apr 29, 2010 · 2 comments
Closed

adding in context with with #344

weepy opened this issue Apr 29, 2010 · 2 comments

Comments

@weepy
Copy link

weepy commented Apr 29, 2010

This has been discussed before, but I was playing around with the following which might be a way of getting access to a current context. It's made possible by the fact that CoffeeScript will declare all variables on a scope. So instead, we can define them on an object 'context'

(function() {
  var context = {x: null, y: null, X: null}
  var x = 5
  var z = 1234
  with(context) {
    x = 1
    y = 1
    X = {a:3}
    
    // mixin
    for(var i in X)
      context[i] = X[i]
    
    console.log(context)   // #object ...
    console.log(x)         // 1 
    console.log(z)         // 1234 
    console.log(typeof h)  // undefined
    console.log(a)         // 3
  }
})()

Anyway - I'm not entirely sure if it's useful beyond mixing into the current scope, but I thought I'd share it anyway. I imagine that it wouldn't be used unless the script specifically mentioned some kind of 'global' keyword.

@noonat
Copy link
Contributor

noonat commented Apr 29, 2010

Other arguments aside, the with statement is not allowed in strict mode ES5, so I'm not sure this is advisable.

@jashkenas
Copy link
Owner

Yep, you can certainly make dynamic local variables by using with or eval, but as we've discussed before, neither of those are practical options for reasons of both performance and standards-compliance.

Closing as a wontfix. Just to make my personal position clear, I can't imagine a situation in which I'd be in favor of a CoffeeScript feature that depended on with or eval, nor would I be comfortable using a JS library of any kind that depended on them heavily.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants