SyntaxError: Use of const in strict mode


#1

I import a javascript file in my ionic2 app in this way,
> import WSPlayer from './assets/js/rtsp_player';
In the browser anything is working fine, but run the same code on android emulator I get this error:

Runtime Error
Use of const in strict mode.
Stack
SyntaxError: Use of const in strict mode.

I’m not sure if I should use the crosswalk browser to resolve this issue, and if this is the only way to resolve it?


#2

@mburger81 Could you fix this issue?


#3

I had a similar issue, created by my own code. I found it using genymotion emulator and chrome://inspect which directed me to the right line in main.js.

I had a file which I import in the code:

export const MENU_SECTIONS = { HOME: 'HOME', GIFT: 'GIFT', FAVOURITES: 'FAVOURITES', ACCOUNT: 'ACCOUNT', SETTINGS: 'SETTINGS', SHOP: 'SHOP', SUPPORT: 'SUPPORT', ABOUT: 'ABOUT', LOGOUT: 'LOGOUT' };

Replacing with this code solved the issue:

export const Global = Object.freeze({
  MENU_SECTIONS : {
    HOME:          'HOME',
    GIFT:          'GIFT',
    FAVOURITES:    'FAVOURITES',
    ACCOUNT:       'ACCOUNT',
    SETTINGS:      'SETTINGS',
    SHOP:          'SHOP',
    SUPPORT:       'SUPPORT',
    ABOUT:         'ABOUT',
    LOGOUT:        'LOGOUT'
  },
});


#4

I’m just going to reiterate my opinion that const in JavaScript is less than worthless.


#5

Hi chris08002, no we are still using crosswalk browser to resolve this. No idea if something changed in the last months.


#6

You are right man, but what should we do if we use three very very big and very very big javascript libs for video decoding and others, where they use CONST and for sure we would not and could not change this, test this and mantain this.
Probably we can remove only CONST but we try to not fork any lib.

IMO
thx


#7

That’s cool, I just don’t want people seeing this, thinking const is of any value whatsoever, and using it in their own code. All it provides is a false sense of security, which is why I consider it “worse than useless”.


#8

@rapropos It’s distracting from this thread a bit, but what would you suggest to use for global values (“constants”) that we want to set throughout an angular/ionic app. Like for the “export const Global” as above?


#9

If I really want to make sure it can’t be modified, I use functions:

export function totallyImmutable(): string {
  return "can't touch this";
}

#10

It seems using app-scripts 3.0 we are able to use es6 and es7 javascript files into the project and can bundle it with --prod flag!