Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Set a 'let' variable from another module in browser

Specifically I'm testing this in the latest version of Google Chrome, but I suspected this would be the same in Edge/Firefox/Safari (Last I was aware Edge and Firefox had modules hidden in experimental javascript flags)

In a 'globals.js' I have a let scale.

// inside global.js
let scale = 1;

In another module, I wanted to adjust the scale

// inside another js file
import { scale } from "../JS/globals.js";
scale = Math.min( scale + ((scale + delta) * speed), target);

But I'm getting error "Assignment to constant variable".

I imagined one obvious way to do this, is to export a function named set_scale

// back in globals.js
export function set_scale (value) { scale = value };

And I have already done this for a few other variables from globals.js. But I wanted to know if anyone has any other methods for overcoming exports/importing lets as constants, especially if it will allow me to avoid making more things to export/import.

like image 765
BetaRuler Avatar asked Sep 17 '26 22:09

BetaRuler


1 Answers

You can do as following in your globals.js file:

export default {scale: 1} 

and in the other modules, use it like this:

import global from "global" 

global.scale = ...
... = global.scale
like image 106
NatNgs Avatar answered Sep 20 '26 11:09

NatNgs