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

  • SEARCH
  • Home
  • 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 7006899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:32:13+00:00 2026-05-27T21:32:13+00:00

I am not sure if I am getting it right. This example is straight

  • 0

I am not sure if I am getting it right.

This example is straight from MDN (Mozilla Developer Network):

var bValue;  
Object.defineProperty(o, "b", {get : function(){ return bValue; },  
                               set : function(newValue){ bValue = newValue; },  
                               enumerable : true,  
                               configurable : true});  

What happens is – it creates a global variable named bValue, which is not done. I understand that this example only demonstrates the use and thus it is okay if it creates a global variable. But if I am going to use this in an application, I will modify it slightly, by adding the this keyword:

Object.defineProperty(o, "b", {get : function(){ return this.bValue; },  
                               set : function(newValue){ this.bValue = newValue; },  
                               enumerable : true,  
                               configurable : true}); 

Now, the object o will have property b, and at the same time, it will also have another property bValue. The user (programmer) will be exposed only to ‘b’ and not to ‘bValue’ though he can still access bValue directly – I don’t see how it can be prevented.

I understand that the property b and the property bValue may not always be same, but b would depend on the value of bValue because the getter and setter allow us to pre-process bValue before assigning the value to b.

The main question is, am I getting it right? Or am I missing something here?

  • 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-05-27T21:32:14+00:00Added an answer on May 27, 2026 at 9:32 pm

    You seem to be looking for a closure. This is a coding pattern that enables you to use private variables and only expose what you want to expose (public variables).

    (function() {
        var bValue;
    
        Object.defineProperty(o, "b", {get : function(){ return bValue; },  
                                       set : function(newValue){ bValue = newValue; },  
                                       enumerable : true,  
                                       configurable : true}); 
    })();
    

    It creates a function and executes it immediately. This seems useless, but since functions introduce a level of scoping, bValue is not accessible anywhere but within the function this way.

    The o.b property acts as a delegate between the developer and the value. You cannot access bValue itself. (Though in this example the getter/setter obviously act such that they exactly do the same as using bValue directly.)

    http://jsfiddle.net/W4CSE/2/

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

Sidebar

Related Questions

I'm not sure why I'm getting this error, but shouldn't this code compile, since
Ok I'm working on getting better with python, so I'm not sure this is
Not sure whether this is the right place, but I have a question related
We are getting a weird issue on which we are not sure what exactly
I'm not sure if I'm missing something really obvious, but I keep getting a
I am getting unexpected T_TRY, expecting T_FUNCTION error message and am not sure as
I'm reading about connection pooling in .net and i'm not sure if i'm getting
I'm having trouble getting a JTextArea to scroll. I'm not sure how you can
I am getting an error message, and I not quite sure where the issue
Not sure if this is possible or if I'm expressing correctly what I'm looking

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.