2

I currently use this style to create a js class like structure:

   var JSClass  =   (function(){
       console.log('JSClass Init');
       //-- Set up private var and fnc here
       var opt  =   {
              width:    0,
           height:  60
           }

       function _PrivateSum(g){
           return (g * opt.width);
       }

       //-- Set up public access here
       function JSClass(){ //the class constructor
           //-- class attributes

       }
           //-- class methods
       JSClass.prototype    =   {
           getWidth : function(){
               return _PrivateSum(opt.width);
           },
           setWidth : function(w){
               console.log('JSClass setWidth: ' + w);
               opt.width    =   w;
           },
           getHeight : function(){
               console.log('JSClass getHeight');
               return opt.height;
           },
           setHeight : function(h){
               opt.height = h;
           }

           };

           return JSClass;

   }());

init by calling the following in another page:

   var jc   =   new JSClass();

This is all good etc but if I then need to create a class that I would like to use several times on the same page: var jc = new JSClass(); var jc2 = new JSClass();

At present if I change anything within the first "jc" then it also controls what is in the second "jc2".

So my question is how would I go about creating a fresh instance of my JSClass() class so that i can manipulate each one individually with out effecting the current one, similar to php classes etc

I believe I would need to somehow create a clone of the original but am not sure, or if there is a better way than above please feel free to inform me

much appreciated

3
  • You should take a look at this article : ejohn.org/blog/simple-class-instantiation . Otherwise, I'd say to use return new JSClass in your class. Commented Feb 23, 2012 at 10:11
  • Inside the closure you overwrite the var defined on the outside "JSClass" - just pointing out this detail, would not do it this way at all myself - see the references (links) that will inevitably be given here. (This overwrite doesn't mean that whatever you want to achieve would not work - the computer won't be confused, but HUMANS reading the code are.) Commented Feb 23, 2012 at 10:14
  • Also recommend checking out this class from the JavaScriptMVC project javascriptmvc.com/docs.html#!jQuery.Class It's based on the simple class by John Resig mentioned by Florian in the first comment Commented Feb 23, 2012 at 11:02

3 Answers 3

1

I like to use a construction like this: note: no new-statement.

//definition
var myClass = function() {
    var abc = 1; //private properties

    function f1() {...}; //private methods

    return {
        bar: function() {} //public function *with* access to private members and functions
    };
};

//usage:

var myInstance1 = myClass();
var myInstance2 = myClass();
Sign up to request clarification or add additional context in comments.

2 Comments

I scream, yes: THIS IS NOT A GOOD EXAMPLE TO COPY. The BIG disadvantage: This wastes a lot of memory! For EACH INSTANCE the private functions are copied. That's why the prototype is a better place. Better sources: look for (google) classical, parasitic, and mixed inheritance (the mix is best). Modern JS libs like YUI show how it's done (on their web pages and in their code). A GREAT source are the presentations from Douglas Crockford on developer.yahoo.com/yui/theater (it is general JS stuff, not specific to the particular JS lib, YUI3, which Yahoo makes).
i agree. but i think it depends on the usecase if you have tons of instances of the class it might be better to use a declaration the utilizes the prototype.
1

All Your class instances will use the same opt object, so changing it in one instance will change it for all other instances, too.

You'll have to move opt into the constructor function. The prototype functions than loose access to opt, of course. If you want to use the functional approach for classes with private members, you have to give up the beauty of the prototype, and inheritance will be complicated. But you'll get real private members.

Crockford's "The Good Parts" is a reading I would recommend for these things.

Comments

1

Your variables ("opt") are STATIC (class variables - i.e. shared across all instances of the class) not instance variables. Instance variables are properties on the "this" object, which you create in the constructor and/or the two set(ter) functions you have. In setWidth,setHeight replace opt.width (or height) with this.width (or height) and remove the static var "opt".

Also, move _PrivateSum into the prototype object or you willo have trouble accessing the new instance variable just introduced - unless you call it using _PrivateSum.call(this, this.width), because when calling it as you do now "this" will be wrong, but if it's an instance method and you call it with this._PrivateSum(...) inside it "this" will point to the correct object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.