Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
Importing a variable with import or defining it with const in the module has a behavior that I do not understand

I have been playing with this issue for a while, I am placing some styling variables (blue, red, yellow) in a separate file and I am importing them in a module to use them.

I have a problem understanding how we store the values there.

1) import statement
If I use the import statement, I can get the values in my class with a console.log(blue), console.log(eval(blue)) but I can't read it with console.log(eval(formColor)). formColor is a string that is equal to "blue", "red", or "yellow".

I get this error for console.log(eval(formColor)) and can't explain why: enter image description here

2) using const
If I define the styling variables directly at the beginning of my file, before the class definition, I can access them with a console.log(blue/eval(blue)/eval(formColor))

My question is, I want to reuse these variables somewhere else so they should be store in another file to make the code clear. How can I achieve that?

FYI, I am using Rails and Stimulus on this project.

radio_style.js (file were I define some styling variables)

const blue = { classToApplyNotChecked: [
"flex",
"justify-center",
"items-center",
"bg-white",
"rounded",
"border-2",
"border-blue-200"], classToApplyChecked: [
"flex",
"justify-center",
"items-center",
"bg-blue-200",
"rounded",
"border-2",
"border-blue-400"],};

form_controller.js (file were I want to import the styling variables in my controller class)

import { Controller } from "stimulus";
// import { blue, red, yellow } from "../plugins_variables/radio_style";

const blue = {...};

export default class extends Controller {
  static get targets() {
    return ["formWrapper", "errorMessage"];
  }
  ...
  styleRadio(){
    ...
    console.log(blue);
    console.log(eval(blue));
    console.log(formColor); // formColor retrieves a string value equal to "blue", "red" or "yellow"
    console.log(eval(formColor));
    ...
  }
}

Directories:
enter image description here

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda