Possible Duplicate:
How to “properly” create a custom object in JavaScript?
Is it possible to construct new types in Javascript? If “everything is an object”, then are object constructors what you use for constructing new types? If so, this makes object constructors also type constructors, right? The help is very much appreciated, and good morning Stack Overflow!
You can use following code to create new Class and add methods and properties to that.
function ClassName() { //Private Properties var var1, var2; //Public Properties this.public_property = "Var1"; //Private Method var method1 = function() { //Code }; //Privileged Method. This can access var1, var2 and also public_property. this.public_method1 = function() { //Code }; } //Public Property using "prototype" ClassName.prototype.public_property2 = "Value 2"; //Public Method using "prototype" //This can access this.public_property and public_property2. //But not var1 and var2. ClassName.prototype.public_method2 = function() { //code here } //Create new Objects var obj1 = new ClassName(); //Access properties obj1.public_property1 = "Value1";You can also extend exiting Classes.
Check on Crockford’s website
Thanks to Glutamat and Felix Kling