5

I want to create a class in Javascript. This class should have

  1. private and public variables and functions.
  2. static and dynamic variables and functions.

How this can be done ?

Triode
  • 11,309
  • 2
  • 38
  • 48
  • 9
    This first step in proper javascript development is acceptance... accepting there is no such thing as a class in javascript... http://javascript.crockford.com/prototypal.html – jondavidjohn Dec 21 '11 at 16:57
  • 7
    So you are down-voting this poor user because he didn't know that JavaScript doesn't have classes? – Šime Vidas Dec 21 '11 at 17:06
  • 2
    @jondavidjohn Your response is implying that the OP refuses to accept, but it's more likely that he simply didn't know... – Šime Vidas Dec 21 '11 at 17:08
  • @ŠimeVidas Your response is implying that I downvoted... – jondavidjohn Dec 21 '11 at 17:14
  • 2
    I agree with Šime Vidas. Just 'cause this question is "wrong" doesn't mean it's not valuable. I'm sure there's a large number of people who would ask the same question. – JHollanti Dec 21 '11 at 17:17
  • I second @JHollanti - I think the community was a bit harsh on this new user – Adam Rackis Dec 21 '11 at 17:20
  • @jondavidjohn No, it isn't. I was referring to the users who down-voted ("you" as a plural). It was meant as a public response. – Šime Vidas Dec 21 '11 at 17:22
  • Its sad that somebody down-voted this question. I know that there is no class in Javascript. But my question is - is there any way to achieve this by using colsure or managing the scope of functions – Triode Dec 21 '11 at 17:29
  • @rajesh.adhi - yes, you can use closures to simulate private variables, as my answer shows. – Adam Rackis Dec 21 '11 at 17:34
  • @rajesh.adhi This might help: http://stackoverflow.com/questions/1181102/javascript-classes – Šime Vidas Dec 21 '11 at 17:39
  • really in javascript if we declare a variable or an Object it is getting added as a property to the window that means every values objects and variables in javascript is having an Object path, so my question is then how we can say that java script is not an Object Oriented Language ? – Triode Feb 16 '12 at 07:14
  • 1
    @RajeshCP: I am not that much aware about javascripts, but found intresting in the question when google it got this link, looks like this one is also looking for what you need. ;) http://stackoverflow.com/questions/387707/whats-the-best-way-to-define-a-class-in-javascript – Dipak Mar 16 '13 at 04:12
  • http://www.typescriptlang.org/ – Konrad Morawski Dec 11 '13 at 13:02

1 Answers1

6

A good way to create objects that support public and private properties is to use a factory function:

function createObj(){
   var privateVariable = "private";

   var result = {};

   result.publicProp = 12;

   result.publicMethod = function() {
      alert(this.publicProp);
      alert(privateVariable);
   };

   //this will add properties dynamically to the object in question
   result.createProperty = function (name, value) {
       this[name] = value;
   };

   return result;
}

As for static, you can simulate them by putting them on the function itself

createObj.staticProperty1 = "sorta static";

And to see dynamic properties in action:

var obj = createObj();
obj.createProperty("foo", "bar");
alert(obj.foo); //alerts bar
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393