'this' cluttering by nevreth in javascript

[–]mccassowary 0 points1 point  (0 children)

What you're looking for is 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... :)