views:

153

answers:

4

Hi there,

Can anyone help? I have the following object in javascript... from what i understand each "INSTANCE" of my calendar will have its own variables.

My question is i need to insert a method/function name called "InitilizeHolidays" this needs to add to an array but the details need to be same in all instances ... I was thinking about some kind of STATIC method call if this is possible ..

Of course if i insert this on "prototype" its going to be specific to each instance and i need for it to effect all instances.

Is it possible to initilise variables that effect ALL instances and ONLY specific instances? Where must i insert these?

Any help really appreciated

function Calendar() {
    // I presume variables set here are available to "ALL" instances

}

Calendar.prototype = {
    constructor: Calendar,
    getDateTest: function() {
        return "date test";
    },
    getDateTest2: function() {
        return "date test";
    }
};
+2  A: 

Yes, it is possible. In yui they use

YAHOO.lang.augementObject(Calendar,{/* Properties go here*/});

But to simplify for you if your not using YUI you can do this

Calendar.MyStaticVar = {/* Any variables you want*/}

That will allow you to define a static variable called MyStaticVar, in this example, its an object, but it could be a string, a number, whatever you want. Then to use it, all you do is go

Calendar.MyStaticVar

The Augment object in YUI is quite nice though, because you can say

YAHOO.lang.augementObject(Calendar,{
   StaticVar1:'somevalue',
   StaticVar2:{/*Object value*/},
   StaticVar3:393,
   SomeStaticFunction:function() {}
});

As opposed to

Calendar.StaticVar1 = 'somevalue';
Calendar.StaticVar2 = {/*Object value*/};
Calendar.StaticVar3 = 393;
Calendar.SomeStaticFunction = function() {};
Zoidberg
+1  A: 

Take a look at this code.

Upper Stage
Very good article.
Zoidberg
A: 

well,I think static property is not for you. consider this:

function MyClass(specialProp) {
    if (specialProp) {
        this.prop = specialProp;
    }
}
MyClass.prototype = {"prop":"defaultValue"};
var foo = new MyClass("changed"), bar = new MyClass();
alert(bar.prop);  //got the property from the prototype chain
alert(foo.prop);  //special property of the instance
5long
+1  A: 

There's something confusing about Javascript's prototype inheritance. Let me explain.

Fields defined at the prototype object are shared by all instances. The problem is that you can't really notice that because assignment into a field of an object o is always carried out at the o instance and not at the prototype.

Thus, you have two options for defining static fields.

(1) Define a field at the prototype object. When you want to change it you must change it through the prototype object and.

  function Calendar() {
  }

  Calendar.prototype = {
     constructor: Calendar,
     y: 'y',
  };

  function go()
  {
     var c1 = new Calendar();
     var c2 = new Calendar();

     alert("c1.y=" + c1.y + " c2.y=" + c2.y);

     // now setting y to 'YYYYY';
     Calendar.prototype.y = 'YYYYY';

     // both c1 and c2 'see' the new y value
     alert("c1.y=" + c1.y + " c2.y=" + c2.y);  
  }

The danger with this is you may accidentally try to set the y field via one of the instances, as in: c1.y = 5555, in which case the assignment will take place on the c1 object but not on the c2 object.

Hence, you can use the second option which is safer, but requires some more keystrokes...

(2) Use Javascript's encapsulation trick to make sure the prototype field is only accisble via getter and setter methods.

  function Calendar() {
  }

  function initCalendarPrototype()
  {
     var y = 'y';
     Calendar.prototype = {
         constructor: Calendar,             
         getY: function() { return y; },             
         setY: function(arg) { y = arg; },                    
     };
  }

  function go()
  {
     initCalendarPrototype();

     alert("c1.getY()=" + c1.getY() + " c2.getY()=" + c2.getY());

     // now setting y to 'YYYYY' via setY()
     // Can be invoked on either c1, c2 or Calendar.prototype
     c1.setY('YYYYY') 

     // both c1 and c2 'see' the new y value
     alert("c1.getY()=" + c1.getY() + " c2.getY()=" + c2.getY());
  }
Itay
I prefer this over initCalendarPrototype:[code]function Calendar () {}Calendar.prototype = (function () { var y = 'y'; return { constructor: Calendar , getY: function () { return y; } , setY: function (newY) { y = newY; } };}) ();[/code]
trinithis