JSFiddle - React, Tailwind, and code Playground
JavaScript
var MessageBox = (function() {
var privateProtoVar = "Hello";
function MessageBox(message) {
var privateInstanceVar = message;
this.instanceMethod = function() {
alert(privateInstanceVar); // Can access private instance var
alert(privateProtoVar); // Can access private prototype var
}
}
MessageBox.prototype.Show = function() {
alert(privateProtoVar); // Can access private proto var
// but can't access privateInstanceVar
}
return MessageBox;
})();
var msg1 = new MessageBox("1"),
msg2 = new MessageBox("2");
msg1.instanceMethod(); // "1", "Hello"
msg2.instanceMethod(); // "2", "Hello"
msg1.Show(); // "Hello"
msg2.Show(); // "Hello"
msg1.newMethod = function() {
alert(privateInstanceVar);
}
msg1.newMethod(); // error