I want to export a single variable that I will increase. My TypeScript example works very well:
main.ts
import { myFirstFunction, mySecondFunction, counter } from "./functions";
function main()
{
console.log("main");
myFirstFunction();
mySecondFunction();
console.log(`counter = ${counter}`);
}
main();
functions.ts
export let counter = 0;
export function myFirstFunction(): void
{
console.log("my first function");
counter++;
}
export function mySecondFunction(): void
{
console.log("my second function");
counter++;
}
Output:
main
my first function
my second function
counter = 2
But how to translate this example to ES5 and Browserify? I do not want to export an object because I do not want to use a prefix like counterInfo.counter. I want to use counter. But when I translate this example I get counter = 0:
You should be able to use exact the same syntax like in typescript. You only have to remove the return type definitions from the methods.
This works fine for me:
import { myFirstFunction, mySecondFunction, counter } from "./functions.js";
function main()
{
console.log("main");
myFirstFunction();
mySecondFunction();
console.log("counter" + counter);
}
main();
export let counter = 0;
export function myFirstFunction()
{
console.log("my first function");
counter++;
}
export function mySecondFunction()
{
console.log("my second function");
counter++;
}
This is a solution. I wanted a global variable for whole project without prefix like global, or parentheses. I know that global variables are bad. But I just need to initialize Ammo.js and WebGL in one place and use Ammo and gl variables in the rest project files. counter is just for example.