Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Define a global read-only, hidden property.

 

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
try {
  globalConst(sName, vVal);
} catch(e) {
  // likely a TypeError if an existing non-configurable property of the same sKey is defined
}

Parameters

ParameterTypeMandatoryDescriptionUtil.js version
sNameString(tick)

The name of the global constant reference

0.8
vValVariant(tick)The value associated with the constant reference0.8

Return value

If a non-configurable property of the same name is already defined, an error will likely be thrown.

Example

Code Block
themeRDark
languagejavascript
titleSame as using 'const' declaration in global scope
linenumberstrue
globalConst("foo", "bar");
 
// same as:
const foo = "bar";
Code Block
themeRDark
languagejavascript
titleCreate global constant from within a function
linenumberstrue
function wibble() {
  const foo = "bar"; // error; in future might create const within scope of wibble()

  globalConst("foo", "bar"); // creates global "foo" constant with value "bar"
}
Div
classbox

Availability

This feature requires:

  • Util.js v0.8 and above
Div
classbox

See also

Related articles:

  • global – reference to the global object/scope