JS Class with private members

Tested in IE9, Chrome, Firefox

by electricvisions

JavaScript

// http://javascript.crockford.com/private.html

function Constructor() {
    var that = this
    var privateVar = "private var"
    
    function privateMethod() {
        document.writeln("<p>private method calling that: " + that + '<p>')
    }
    
    this.publicMethod = function() {
        privateMethod()
        document.writeln("<p>privileged method</p>")
    }
}
        
var c = new Constructor()
c.publicMethod()