Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Using javascript eval() for private/self use functions. Should I avoid it?

So I have an object. And the object has 6 variables (a, b, c, d, e, f)

Something like this:

let obj={
   a:0,
   b:0,
   ...
   ...
   }

I am passing this object as a function parameter and the function has to set the values of all the variables (a to f). The values are simply copied from other variables (lets call them u, v, w, x, y, z).

Based on the user's (my own) choice, a could be mapped to w or y or z etc and so on. Currently I am doing this manually like this:

   obj.a=x;
   obj.b=y;
   obj.c=w;
   ...
   ...

The issue is, I want this mapping pattern to be very flexible. I want to change the entire mapping with the least change of coding and I want the coding to be as minimal/clean as possible. Currently there are 4 possible mapping options based on how I want to do it at runtime. But I want to add more options and as mentioned above, I want the mappings to be very change-friendly.

Should I declare a mapping string and use eval to automate the mapping? Then I would have to simply change the string arrangement and the values will be mapped automatically. Something like this:

   var mapping="xyzuvw", values="abcdef";
   for(var i=0;i<6;++i)  //using 6 since mapping string is of 6 chars
      eval("obj."+values[i]+"="+mapping[i]);

As you can see, I only have to change (or create a new) mapping string and all the mapping sequence is automated.

The software is intended entirely for my own/private use.

Should I use eval() to automate this mapping process or should I manually edit (and create new) the mapping coding if I want to change the mapping pattern?

As in, will using eval() make the code prone to processing hiccups, runtime errors etc?

Will maintaining the code be easier in future with eval() or with manual mapping?


Edit to add:

For members who are referring me to Dynamically access object property using variabl, please note that I am trying to automate target variable name (whose value we want to copy) and not base variable name (whose value we want to change). As in, I want to automate whether obj.a = x; or whether obj.a=y;

Thanks.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!