[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 640
  • Last Modified:

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

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();
  • 2
1 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.
dvcphpAuthor Commented:
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.
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now