Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper to write a global const in javascript es6?

Tags:

javascript

What is the right way to write a global const in a script in javascript es6 and use it in some other script?

like image 779
phongyewtong Avatar asked Nov 28 '22 23:11

phongyewtong


2 Answers

You just use const at global scope:

const aGlobalConstant = 42;

That creates a global constant. It is not a property of the global object (because const, let, and class don't create properties on the global object), but it is a global constant accessible to all code running within that global environment.


It's probably worth noting, though, that the trend is to move away from globals, and in fact even to move away from having any code run at global scope (although top-level scripts in browsers will always have access to global scope, for compatibility).

For instance: In NodeJS, none of your code is running at global scope; everything (including the main script you run via the node command) is in a NodeJS module environment, which is not global scope. So in NodeJS, the line above wouldn't create a global, because it's scoped to the call to your module. To create a true global in NodeJS (which usually isn't necessary), you have to create a property on the global object. But how to access the global object? NodeJS calls your code with this set to an empty object (rather than the usual loose mode default where this refers to the global object), and of course there's no window in NodeJS. Answer: NodeJS provides a global available for that: global

// Define a global constant in NodeJS
Object.defineProperty(global, "aGlobalConstant", {
    value: 42
});

That's a constant because by default, properties created via Object.defineProperty are read-only.

ES2015's modules take it a step further than NodeJS: You cannot create a global at all unless the environment makes a global like NodeJS's global available to you (in which case you can do the Object.defineProperty thing). By default, per spec, there is no such global; it's just environments that define them (window on browsers, global in NodeJS, ...). And the top-level code in an ES2015's module is not run with the old default of this referring to the global object (this has the value undefined at the top-level scope of an ES2015 module), so you can't use this for it.

like image 93
T.J. Crowder Avatar answered Dec 01 '22 13:12

T.J. Crowder


Great answers above especially the note about "const, let, and class don't create properties on the global object".

Just to add, beware of implications of that as relates to IFrames.

In an IFrame you can access the variables defined in the parent-window (if they come from the same domain) by referring to "parent.someVar".

But because 'const' or 'let' or 'class' won't add to the properties of their global object the iFrame can not access variables defined by const or let in the parent-window. But you CAN if you have defined them as variables with 'var', in the parent window.

In that way 'var' gives you a more global reference than 'const' or 'let'. If you need to create a variable that is available in sub-frames you need to use 'var' to declare it it seems. And but then it will not be a "constant".

So one way to answer your question is to say that while you can define "global variables" by using 'var' you can not define (truly) "global constants" because if they are declared with 'const' they won't be visible in iFrames, if they are defined with 'var' they won't be constants.

Unless there's some way to access consts defined in the parent-window. I haven't found one. Is there?

like image 30
Panu Logic Avatar answered Dec 01 '22 12:12

Panu Logic