How can I create a class with public,private and priviledge members and methods in javascript?

Posted on 2012-08-29
Last Modified: 2012-09-05
Hello Experts,

I know there is no concept of classes in javascript but we can fake them using prototypes.

I wanted to make a fake class called car in javascript.I have written some code.Please check and let me know if this is corerct.I appreciate.Thanks in advance.

1.      Public method
a.The members of the objects are all public.Any function can acces,modify,delete and edit those members.

function Car(model,color){
this.member = model;
this.col = color;
Var myCar = new Car(‘toyota’,’Grey’);
alert(myCar.member);//alerts Toyota

b.) Another method is to use Prototype.It adds public methods
Car.prototype.available = function(string){
return this.member + string;
alert(myCar.available(‘ yes !’)); // output: Toyota yes !

2.Using Private Members:

function Car(){
function available(){
If(color == red){
return true;}
return false;
this.member = model;
 var color = “red”;

        The private method available examines the color instance variable.If it is                 true,red is returned.
The pattern of public,private and privileged members can be implemented using closures.

3.Example using Closure.

function car(){
            var model = "Toyota";
            var checkValue = function(){alert(model);}
            return checkValue;
       var pointerCheck = car();
Question by:dvcphp
    LVL 33

    Accepted Solution

    greetings   dvcphp, , ,  I am kind of reluctant to comment on the "private, priviledge, and protected" settings for Classes-Objects that other programming languages incorporate. But the question is kind of interesting, as something to consider, so I'm here.  I looked at your code examples above, and really do not see any correspondence to the  "private, priviledge, and protected" restrictions of some Class definitions that are not javascript. Many languages allow functions with-in functions to use an "access Scope" for the inner function that includes the container function, but this does not duplicate the private designation as I do not think  you can do the inner function from the this.innerfunction('no work"); of the Class object framework. As I see it, javascript is designed to be a very flexible  scripting language, and allows you to re-define any function or object at any time as the web page dynamically changes. So the private and protected designations are not possible, or even desired, for code that MUST be able to change at any time, allowing what a function does, or what an object does to be completely different as elements and code are added or removed from a web page. If there was a "Private" restriction allowed, by definition, it should not be allowed to be accessed or changed from outside the Object framework, and would cause problems if code needed to change or be revised with changes in the page. Although I don't see many in javascript. changing their functions and objects that much, once they are set up, it is part of the "Idea" for  a dynamic language. That's my take on it anyway.

    I wonder why you want to have "private, priviledge, and protected" properties in a javascript Object instance? ? Seems like a waist of time, even in many languages that allow that, it's often used from old habits, not real necessity.

    Author Comment

    Thanks Slick812,
    Since js is an object oriented client side scripting language,
    I was trying to use private, public and privileged .Object oriented language have the concept of private , public and protected.But , you are right it's an old habit.
    I am glad js is such an easy language to learn.
    LVL 33

    Expert Comment

    The start of javsscript is relatively easy, but there's many things that are not obvious, especially if you've worked in other programming languages,  for example, I had much trouble in objects, because javascript Always treats array variables as a "reference" or a pointer back to the original array, when you pass an array in a function parameters. So, some experience, gets you into the "not so easy" place.  But for me , when I realized that I could change any function with -

    function doThis( ) {
        alert("do this function");

    //  re define the function

    doThis = function(inStr) {
        inStr += " added this";
        alert( inStr);

    It allowed me to not many have if checks in functions, if the setting changed, I could redefine the function instead of checking each time for some value, like if it's IE, chrome, or firefox.

    But I will say that class-objects in javascript, are unusual containers for the "Scope" of variables, you can have a this.prop in a object-class function , and if that function is called in a system call (like asynchronous ajax), then ALL reference to the "this" is lost, and the function fails to do the job, because the function is treated as if it is not in a object..

    Featured Post

    Live - One-on-One JavaScript Help from Top Experts

    Solve your toughest problems, fast.
    JavaScript experts are online now and ready to help you.

    Join & Write a Comment

    Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question ( on how to make a page show some balloons animate up a page…
    Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now