Dave's Brain

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

Date: 2009jul22
Keywords: OOP, OO
Language: javaScript


Q.  Can you explain Object-Oriented javaScript?

A.  ECMAScript 6 added real support for clases.  Until 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 var PRIVATE_MEMBER_FUNCTION = function(param1, param2) { // This member function can NOT be accessed from outside the class // Do stuff }; // <--- Note the semi-colon! var 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);

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.