0

With ES6 and the various other upgrades to JavaScript, is there a way yet to dynamically declare variable names as (or) references to object properties, so that we do not have to constantly refer to the object.property syntax to access property values?

For example this syntax:

var obj = {a:1, b:2};  console.log(obj.a + " " + obj.b); // 1 2

or 

var obj = {a:1, b:2}, a = obj.a, b = obj.b; console.log(a + " " + b); // 1 2

Can it be converted to something like the following which can dynamically declare or reference each variable name based on the object's properties without having to declare them separately or so that each property reference doesn't have to specify the object name?

var (...obj); console.log(a + " " + b); // 1 2

The goal being that the console.log statement will work without having to reference the object or declare the variables individually?

I've read through the newer capabilities but am not finding anything to make this easier / shorter.

ciso
  • 2,887
  • 6
  • 33
  • 58
  • Check out [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) `const {a, b} = obj;` – Gabriele Petrioli Apr 19 '22 at 19:49
  • Here you are listing the property names. a, b. The question is: Is there a way to do this without listing the names of the properties so that all the properties in the object are accessible as simple variable names. For example {} = obj; console.log(a + " " + b); works. I've edited the question to make this clearer. – ciso Apr 19 '22 at 19:52
  • 4
    Nope, and rightfully so, as that would very easily cause side-effects and get out of hand. You need to be explicit with variable names or you risk ambiguity. – Gabriele Petrioli Apr 19 '22 at 19:54
  • If carefully used, it would be manageable, imho. – ciso Apr 19 '22 at 19:55
  • @ciso If you're careful, just type out the few variable names. How many variables are we talking about? And it's not just an optimisation, it also improves code readability if it is clear what variables are declared where and with what values. – Bergi Apr 19 '22 at 20:27
  • @Bergi. That's the issue. The object has various properties which are / can be different every time the function is called. Within the function there are conditional statements that test one property to see what type of object it is. Then the if statement applies specific formulas to the variable names for that type of object. The thing I'm trying to solve is to stop using "obj." every time when it's the same object but just different property names with each function call. Looking for a way to shorten the code removing all the "obj." prefix references when it's always the same obj name. – ciso Apr 19 '22 at 20:33
  • So do `if (obj.type == '…) { const {a} = obj; … } else if (obj.type == '…) { const {b} = obj; … }` where you destructure the properties that the respective object type has – Bergi Apr 19 '22 at 20:38
  • There are hundreds of properties. So this would be a lot of extra code to accomplish the goal of shortening the code. I'm using obj.a, obj.b, etc... today. Would like to use a and b without a lot of code in front of it to make this possible. A generic de-structuring or "with" type statement (one not deprecated) would be useful for this. Makes the code shorter and more readable. – ciso Apr 19 '22 at 20:42
  • Also, each property is often only referenced one time in the conditional formulas. So it wouldn't make sense to de-structure specific property names just to use it one time. – ciso Apr 19 '22 at 20:47
  • Hundreds of properties that you need to individually reference? What are you doing? I guess writing `obj.x` makes most sense for properties that you use only once anyway - nobody could keep in their mind which of the many variables in your code come from `obj` and which don't. – Bergi Apr 19 '22 at 23:49
  • They all come from the one object. No other declared variables. Any one object will only have a few of the properties per function call. However the total variety of property names is large as are the variety of formulas. They are referenced via the conditional statements resulting in a unique formula being applied to a result property in the same object passed. Anyway, it would be useful in this case which is why I submitted the question. Apparently others asked the same previously. Thank you for the responses. I have the answer now, which is no... at least not yet available. – ciso Apr 20 '22 at 03:35
  • 1
    You are basically looking for the [`with` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with), but it's deprecated and doesn't work in strict mode. – Felix Kling Apr 20 '22 at 11:19

0 Answers0