• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

155
Views
How to evalute variables in a function before converting to string in JavaScript

I want to evaluate all the variables inside a function before converting the function to a string.

Example:

const x = "5";

function someFunc(){
   console.log(x);
}

someFunc.toString()

Output:

function someFunc(){
   console.log(x);
}

Expected output:

function someFunc(){
   console.log("5");
}

I need it to work dynamically for any number of variables and any function without knowing the function or the variables first. I prefer a solution based on metaprogramming such as reflection, but any working solution is good, including Regex.

Any ideas on how to implement that?

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I don't think this is possible, at least not exactly how you describe it.

The key blocker is that there is no way to get a list of all currently-visible variables, or their values.

So at best you'd have to change the scenario a bit: start by reading a file (or other string) containing the source code you want to process. Then use a JavaScript parser (e.g. Esprima; no endorsement, just illustration) to get an AST (abstract syntax tree). Then you can do whatever transformations you like on that AST (such as: replacing constant variables with their value), and then convert the AST back to a string (which you can then write to a file, if desired).

The above should work for simple cases like the example you gave. When you look at more complicated scenarios, you'll run into obstacles and limitations though. For example:

const x = 5;

function Case1() {
  let y = x;
  if (someCondition()) y++;
  console.log(y);  // what now?
}
function Case2() {
  for (let i = 0; i < x; i++) {
    console.log(i);  // what now?
  }
}
function Case3() {
  const y = {foo: 42, valueOf() { return "null"; }};
  console.log(y);  // what now?
}
function Case4() {
  const y = Math.random() > 0.5 ? "yes" : "no";
  console.log(y);  // what now?
}

I'm sure there are plenty more situations where this idea is difficult to put into practice.

You didn't say what your ultimate goal is, and this does have the smell of an xy question. Are you looking for optimization opportunities? A debugging environment? I strongly suspect that there's an easier way to accomplish whatever your actual goal is.

(Also, this doesn't have anything to do with V8, libuv, or Node.js; I'll drop tags accordingly.)

almost 3 years ago · Juan Pablo Isaza Report

0

const x = 5;

function someFunc(){
    // do something with x
    return x;
}

someFunc().toString();

Or

const x = 5;

function someFunc(){
    // do something with x
    return x.toString();
}

someFunc();
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error