Ok, so this has got me stumped, hoping one of you maybe can help me out. So, I have an app that is using capacitor to build. In my app I have global JS variables for example:
//globals
var isCheckin = "";
var isUser = "";
etc...
On android, this works just fine (and previously, did work on IOS). But now, when I go to build it on IOS and run the application, my if statements for stuff like:
if(isCheckin !== "") {}
No longer work, because isCheckin is coming back as undefined.
I was doing some searching and came across 'hoisting' and while I guess that may be the case why would:
undefined?In-fact, the current version of the app, in app store still works just fine, but new builds are having this issue when global variables seem to get ignored by safari.
Your definition of global variable should be defined.
Let's see this first example:
const myFunction = (() => {
var foo = 'bar';
});
myFunction();
console.log(foo);
The variable is getting defined in the scope of a function. I.e. outside that scope it will be undefined.
What you can do, is removing var from it. See:
const myFunction = (() => {
foo = 'bar';
});
myFunction();
console.log(foo);
Now the variable is global and can also be use outside the function. BUT if you set "use stricht", the variable will be undefined. See:
"use strict"
/* global console */
const myFunction = (() => {
foo = 'bar';
});
myFunction();
console.log(foo);
So the cleanest way to get the value of the variable outside the scope is using let. See:
"use strict"
/* global console */
let foo;
const myFunction = (() => {
foo = 'bar';
});
myFunction();
console.log(foo);