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 8303291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:29:30+00:00 2026-06-08T17:29:30+00:00

I came up with the situation below: function Dog () { use strict; this.age

  • 0

I came up with the situation below:

function Dog () {
    "use strict";
    this.age = 1;
    var name = "Fido";
    this.getName = function () { return name; }
}

And now I’m creating a new instance of the “Dog” class and printing the variable’s values.

var d = new Dog;
document.write('<strong>Dog age:</strong> ' +d.age); \\Outputs "1" as expected
document.write('<br/>');
document.write('<strong>Dog name:</strong> ' +d.name); \\Outputs "undefined" as expected, 'cause it's a private variable.
document.write('<br/>');
document.write('<strong>Get Dog name:</strong> ' +d.getName()); \\Outputs "Fido", as expected.

But let’s say I want to change the Dog’s name, like this:

d.name = "Stinky";
document.write('<br/>');
document.write('<strong>Dog name Again:</strong> ' +d.name); 
document.write('<br/>');
document.write('<strong>Get Dog name Again:</strong> ' +d.getName());

Based on this, I got a couple of questions:

  1. Why on earth “d.name” didn’t showed me “undefined”? Isn’t “name” a private variable? I suppose you can’t change private variables values, am I right? I wonder if this process have created a new variable, but this time, a public one, and with the same name. If so, is there a way to prevent the creation of new variables every time I try to assign a new property with the same name? Is there a way to throw a “type error” or something (well, this was what I expected).
  2. And at last: Why “getName” printed the original value, “Fido”, even after I assigned a new value to it ?

Any ideas?

Here’s a fid to make things easier.

http://fiddle.jshell.net/yZpfg/2/

  • 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-08T17:29:31+00:00Added an answer on June 8, 2026 at 5:29 pm

    d.name = "Stinky"; is adding a new (public) property to to the d object, which is an instanceOf Dog.

    the getter still references the (private) variable with the value Fido.

    If you want to allow a consumer to change a private variable, you need a setter as well:

    function Dog () {
        "use strict";
        this.age = 1;
        var name = "Fido";
        this.getName = function () { return name; }
        this.setName = function (value) { name = value; }
    }
    
    var d = new Dog(); 
    d.name; // undefined because there is no name public property
    d.getName() // returns the internal private, "Fido"
    d.setName('Stinky'); // the internal private is now "Stinky"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this situation while migrating our DB from Foxpro to SQL. Below
I recently came across a situation where I had to use XPath as part
I came across with this situation. I have one already developed Application in 2.0,
I came across this kind of situation but could not resolve, any hint would
I was just implementing a crude functionality using jsf+richfaces and came across this situation,
I came across a strange situation today where I needed a function to not
I came across this situation where the following plinq statement inside static constructor gets
I haven't actually came across this situation yet but i will probably have a
What if one branch has two heads? I came to this situation weeks ago
In answering this question , I came across a situation that I don't understand.

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.