Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8037191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:51:17+00:00 2026-06-05T02:51:17+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:51:19+00:00Added an answer on June 5, 2026 at 2:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How come every object appears to be marked new, instead of just objects that
I've been trying to come up with a way to declare array constants in
I have 3 classes of task (I, D, U) which come in on a
I have to create a web page which can be set to full screen(I
Lately, I've come across a lot of Java code that relies on properties files
Come across what looks at first sight like an MT-issue, but I'm trying to
How come this works: import datetime now = datetime.datetime.now() month = '%d' % now.month
I come from a C++ background and I've been working with C# for about
I come from the asp.net world where we'd use an objectdatasource, hooked up to
I come from a php background and in php, there is an array_size() function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.