Home:ALL Converter>How to create Javascript constants as properties of objects using const keyword?

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

Ask Time:2012-06-01T10:09:44         Author:danronmoon

Json Formatter

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

Author:danronmoon,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/10843572/how-to-create-javascript-constants-as-properties-of-objects-using-const-keyword
ziad-saab :

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:\n\nvar obj = {};\nObject.defineProperty( obj, \"MY_FAKE_CONSTANT\", {\n value: \"MY_FAKE_CONSTANT_VALUE\",\n writable: false,\n enumerable: true,\n configurable: true\n});\n\n\nRegarding 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.\n\nedit: thanks to @pst for noting that objects literals in javascript are not actually \"passed by reference\", but using call-by-sharing:\n\n\n 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.\n",
2012-06-01T02:14:14
zloctb :

const person = {\n name: \"Nicholas\"\n};\n\n// works\nperson.name = \"Greg\";\n\n\n\nconsole.log(person) //Greg \n\n\nThat's why use Object.defineProperty",
2016-08-02T03:19:54
p0wdr.com :

There is a far simpler way to do this. I like this pattern. Simple Objects.\n\nwindow.Thingy = (function() {\n\n const staticthing = \"immutable\";\n\n function Thingy() {\n\n let privateStuff = \"something\";\n\n function get() {\n return privateStuff;\n }\n\n function set(_) {\n privateStuff = _;\n }\n return Object.freeze({\n get,\n set,\n staticthing\n });\n }\n\n Thingy.staticthing = staticthing;\n return Object.freeze(Thingy);\n})();\n\nlet myThingy = new Thingy();\n\nThingy.staticthing = \"fluid\";\n\nmyThingy.staticthing = \"fluid\";\n\nconsole.log(Thingy.staticthing); // \"immutable\"\nconsole.log(myThingy.staticthing); // \"immutable\"\n\n\nObject.freeze is doing the work here\n\nhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze\n\nif you want you can leave the static property off the instance by leaving it off the object literal return on the constructor function.\n\nconst will only make it a read-only reference. As soon as you assign it, like here in a object literal it becomes a property of the constructed object.",
2017-07-15T10:10:25
Đỗ Công Bằng :

var obj = {};\nObject.defineProperty( obj, \"MY_FAKE_CONSTANT\", {\n value: \"MY_FAKE_CONSTANT_VALUE\",\n writable: false,\n enumerable: true,\n configurable: false // instead of true\n});\n\n\nWe should set also configurable to be false so that it will prevent the property from being deleted from the obj\n\ndelete obj.MY_FAKE_CONSTANT;\n\n\nWith configurable to be true, after the line, we don't have the MY_FAKE_CONSTANT anymore. \n\nReference",
2018-06-09T15:58:00
yy