Browse - Programming Tips - Can you explain Object-Oriented javaScript?

Date: 2009jul22
Updated: 2018jan25
Language: javaScript
Keywords: OOP, OO, class

Q.  Can you explain Object-Oriented javaScript?

A.  ECMAScript 6 added real support for clases.  Until it is widely available
use these tricks.

There are other tutorials on the web but I find most of them too verbose.
Here's the short answer.  UPPER_CASE names are selected by you.

function CLASS() { var PRIVATE_VARIABLE; // This variable can NOT be accessed from outside the class const PRIVATE_MEMBER_FUNCTION = function(param1, param2) { // This member function can NOT be accessed from outside the class // Do stuff }; // <--- Note the semi-colon! const CONSTRUCTOR = function() { // Do stuff }; // <--- Note the semi-colon! this.PUBLIC_VARIABLE; // This variable CAN be accessed from outside the class this.PUBLIC_MEMBER_FUNCTION = function(param1, param2) { // This member function can be accessed from outside the class // Do stuff }; // <--- Note the semi-colon! CONSTRUCTOR(); // Call the private member function called CONSTRUCTOR to initialize }; // <--- Note the semi-colon! CLASS.STATIC_METHOD = function(param1, param1) { // Do stuff }; // <--- Note the semi-colon! // That's it. // Example use: var a = new CLASS; a.PUBLIC_VARIABLE = 25; a.PUBLIC_MEMBER_FUNCTION(1, 2);