-1

I am having problem identifying what and how is some property of "local" scope in JS. How is the property _secretNum "private" when it is clearly available outside the scope of the constructor as per below line.

document.write("Value of secretNum in secret: " + secret._secretNum
            + "<br/>");

The full code is as below,

<body>
<script type="text/javascript">
    function SecretCode() {
        var _secretNum = 78;

        this.guessNum = function(num) {
            if (num > _secretNum) {
                return "Lower";
            } else if (num < _secretNum) {
                return "Higher";
            } else {
                return "You guessed it";
            }
        }

        this.setNum = function(newNum) {
            this._secretNum = newNum;
        }

        this.getNum = function() {
            return this._secretNum;
        }
    }

    var secret = new SecretCode();

    for ( var prop in secret) {
        document.write(prop + " : " + secret[prop] + "<br/>");
    }

    document.write("Is 70 the number: " + secret.guessNum(70) + "<br/>");
    secret.setNum(9);
    document.write("Value of secretNum in secret: " + secret.getNum()
            + "<br/>");
    document.write("Value of secretNum in secret: " + secret._secretNum
            + "<br/>");

    var secretAliter = new SecretCode();
    secretAliter.setNum(17);
    document.write("Value of secretNum in secretAliter : "
            + secretAliter.getNum() + "<br/>");
    document.write("Value of secretNum in secretAliter : "
            + secretAliter._secretNum + "<br/>");
</script>
</body>
VedVrat
  • 103
  • 3
  • 12

1 Answers1

3

How is the property _secretNum "private"

It's not, I don't know where you got that code from, but it's broken.

The local variable declared by var _secretNum = 78 is private, but it's also being ignored by the setNum and getNum accessor methods. They're using this._secretNum, which is a completely different not private variable, that has nothing to do with the var _secretNum = 78 line. You can change the value of this._secretNum all you want, both directly and by using setNum, and it won't affect the variable _secretNum which guessNum uses.

The point of declaring var _secretNum as a local variable within your constructor function is that it is only accessible within that function, and by the functions that "close over" it when they are returned. Drop the this._secretNum from your accessors and just use _secretNum.

By way of example:

function MyConstructor() {
  var _name = "bob"  // Completely private

  this._age = 45     // Not private

  this.getName = function () {
    return _name     // public accessor for private variable
  }

  this.getAge = function() {
    return this._age // public accessor for public property
  }
}


var x = new MyConstructor();
x._name = "blah"    // unrelated, ignored by getName
alert(x.getName())  // bob, not "blah"
x._age = 25         // overwrite age
alert(x.getAge())   // 25
user229044
  • 232,980
  • 40
  • 330
  • 338
  • I wish you had looked at the 1st code snippet before you saw the full code and not kept your answer limited to the getter and setter methods. There are atleast 2 doc.write's which are accessing the so called "private" variable without the getter. – VedVrat Jun 28 '16 at 06:23
  • @VedVrat No, there aren't, please re-read my answer. **None of your doc.writes access the private variable.** Again, there are **two variables** here, and every instance where you *think* you're accessing the private variable, you're not. Your document.writes calls access `secretAliter._secretNum` and `secret._secretNum` which are **different variables** than the private one. Those properties are public, and they are **completely unrelated** to the private variable. The private one is not accessible. You are not accessing the private one **anywhere** in your code, except inside `guessNum`. – user229044 Jun 28 '16 at 12:54