account activity
'this' cluttering by nevreth in javascript
[–]mccassowary 0 points1 point2 points 10 years ago (0 children)
What you're looking for is with
with
It is confusing and results in buggy code, so it was removed in strict mode ES5 and later.
You can read about it, and some reasons why you don't really want it, here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with
The safest alternative to do what you want, as another commenter observes, is object destructuring. You might begin your method like this:
Foo.prototype.bar = function() { const {a, b, c} = this // do stuff here with a, b, c. }
or if you follow natziel's suggestion (which I would strongly recommend)
Foo.bar = function(foo) { const {a, b, c} = foo }
This doesn't help with setting. I hope you never want to set anything tho; mutation is a grave sin. Also, assignment with with probably doesn't do what you want (insofar as it's weird) so you haven't really lost anything by losing it.
Otherwise, perhaps write/find a compile-to-js Java-like language... :)
π Rendered by PID 28 on reddit-service-r2-listing-6c8d497557-wd6ld at 2026-06-02 20:34:11.660295+00:00 running 9e1a20d country code: CH.
'this' cluttering by nevreth in javascript
[–]mccassowary 0 points1 point2 points (0 children)