Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Javascript constants as properties of objects using const keyword?

How come constants cannot be set as properties of objects which are variables themselves?

const a  = 'constant' // all is well // set constant property of variable object const window.b = 'constant' // throws Exception // OR var App = {};  // want to be able to extend const App.goldenRatio= 1.6180339887  // throws Exception 

And how come constants passed by reference suddenly become variable? EDIT: I know App won't (or rather... SHOULDN'T) be mutable; this is just an observation...

(function() {     const App;     // bunch of code     window.com_namespace = App; }()); window.com_namespace; // App window.com_namespace = 'something else'; window.com_namespace; // 'something else' 

How can a nicely organized, extensible, object-oriented, singly namespaced library containing constants be made with these limitations?

EDIT: I believe zi42, but I just have to ask why

like image 414
danronmoon Avatar asked Jun 01 '12 02:06

danronmoon


People also ask

Can you declare a constant with the keyword const?

The const keywordVariables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.

Can you add a property to a const object JavaScript?

It can be assign on the variable on declaration line. Primitive value. The property of a const object can be change but it cannot be change to reference to the new object. The values inside the const array can be change, it can add new items to const arrays but it cannot reference to a new array.

How do we declare object constants in JavaScript?

Introduction to the JavaScript const keyword ES6 provides a new way of declaring a constant by using the const keyword. The const keyword creates a read-only reference to a value. By convention, the constant identifiers are in uppercase. Like the let keyword, the const keyword declares blocked-scope variables.

Can you add properties to const object?

When the content is an object, this means the object itself can still be altered. Therefore, it's possible to change the content of the object that is declared with const variable, but you cannot assign a new object to a const variable. You are still allowed to add new attributes to your object.


1 Answers

You cannot do it with constants. The only possible way to do something that behaves like you want, but is not using constants, is to define a non-writable property:

var obj = {}; Object.defineProperty( obj, "MY_FAKE_CONSTANT", {   value: "MY_FAKE_CONSTANT_VALUE",   writable: false,   enumerable: true,   configurable: true }); 

Regarding your question as to why a const passed to a function becomes variable, the answer is because it's passed by value and not by reference. The function is getting a new variable that has the same value as your constant.

edit: thanks to @pst for noting that objects literals in javascript are not actually "passed by reference", but using call-by-sharing:

Although this term has widespread usage in the Python community, identical semantics in other languages such as Java and Visual Basic are often described as call-by-value, where the value is implied to be a reference to the object.

like image 66
ziad-saab Avatar answered Sep 22 '22 18:09

ziad-saab