//Object oriented javascript with private and public methods. //Declare the class with a constructor - in js this is a function, I know. function className(id) { return { parseValue: function (input) { { return _private(input); } } } function _private(input) { $('#' + id).html(input); } } //Instantiate the class: var myClass = new className('myStuff'); myClass.parseValue('Olaf the snowman is my name, and I love the sun!'); //Declare a second class: function classMangler (id) { this.base = className(id); //Sets constructor of parent } //Let our second class inherit from our first class classMangler.prototype = new className('myVerifiedStuff'); //Test that we have gottent the first class parseValue attribute myClass.parseValue('Olaf the snowman is my name, and I love the sun!'); var myClass2 = new classMangler(); myClass2.parseValue('Olaf the snowman is my name, and I love the sun!'); //Override what we have inherited from the first class to create random Orcish translator! classMangler.prototype.parseValue = function(input) { var a = input.split(""), n = a.length; for(var i = n - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var tmp = a[i]; a[i] = a[j]; a[j] = tmp; } $('#myMangledStuff').html(a.join("")); } var myClass3 = new classMangler(); myClass3.parseValue('Olaf the snowman is my name, and I love the sun!');
<div class="box"> - <span id="myStuff"> </span> <br/> - <span id="myVerifiedStuff"> </span> <br/>And in Orcish: <span id="myMangledStuff"> </span>!! </div>
.box { background-color: orange; } #myStuff { font-style: italic; } #myMangledStuff { margin-left:15px; font-weight: bold; }