1

I just recently found out ( here) that there is a way to make variables in classes completely private in Javascript, meaning not accessible with the dot operator.

It works as such: JSFIDDLE

function PrivateRuns(startOff) {// make an object
    function getRuns() {// private function used to get private variables
        privateRuns -= 1; // accessable with "out" because in same scope
        privateRuns = Math.max( privateRuns, 0);
        return privateRuns + privateHits;
    }

    var privateRuns = startOff;
    var privateHits = 6;

    this.setRuns = function(val){ privateRuns = Math.max(val, 0); };
    this.getAction = function () {// this function is of scope "this"
        //out.privateHits = out.privateHits + 1;
        return ( "runs n hits:  "+ getRuns() +"   "  );
    };
}
var myRuns = new PrivateRuns(10);

myRuns.privateRuns = -10;// does not work
//myRuns.setRuns(-10);// DOES work
for( var i = 0; i < 10; i ++ ){
     $("HTML").append(  myRuns.getAction() +"<br>" );
}

Now I know that if used properly this has a lot of benefits, but is it really necessary? I have rarely ever seen this in tutorials, and I can't say I have personally looked for this in open-source projects.

Regarding this,

  1. Is it actually an error? (the site I read it mentioned it was a "hack")

This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.

  1. Is it a common practice, why or why not?
Andrew
  • 3,393
  • 4
  • 25
  • 43
  • you'll realize something only when you need it. – Amit Joki Apr 12 '15 at 03:41
  • I changed the question a bit to make sure it's not opinion based. So it's not exactly "when should I use this", but *should* I use it at all if it's an error. – Andrew Apr 12 '15 at 03:46
  • I think only the requirement says you should use private variable or not. – Suman Bogati Apr 12 '15 at 03:46
  • 1
    [Closures, like this, are fairly common practice.](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) Though, note that the quote about a "workaround" regards the specific use-case of capturing the value of `this` in a variable for reference in an embedded function, not about closures in general. [`.bind()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) was made available with ES5 as an alternative. – Jonathan Lonowski Apr 12 '15 at 03:56
  • 2
    it's seems really overbuilt. look into ES5 getters and setters for a simpler way of coding the main interaction. – dandavis Apr 12 '15 at 03:58

1 Answers1

0

One reason you might want to use 'private' variables is if you are developing a library that is going to be used by other people. You can encapsulate all of your private variables, functions, etc and expose a public API into your library. This would allow your library to be included onto a page without having to worry about any of your variable names conflicting with other variables on the page. Take for example: jQuery. You can access all of the public API through the variable names jQuery or $. This means that only the variables jQuery and $ have been added to your page so you are free to use any other variable name you want without fear of conflicting with jQuery. The private variables allow you to implement things in your library however you want without fear of some piece of code on the page intentionally or accidentally altering the expected behavior.

It is not an error it is simply a side effect of the way variables are scoped in JavaScript

Check out the concept of immediately invoked function expressions as an example use case where private variables and functions can be useful (http://benalman.com/news/2010/11/immediately-invoked-function-expression/). There are many examples where private variables are used in JavaScript - as previously mentioned JavaScript library such as jQuery use them extensively.

Chad
  • 81
  • 3