Creating a javascript object to emulate a class and static methods?

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" that needs to be added to the array, but the details should be the same in all instances ... I was thinking of some kind of calling the STATIC method, if possible ..

Of course, if I paste this into "prototype", it will be specific to each instance, and I need it to execute all the instances.

Is it possible to initialize variables that affect ALL instances and ONLY specific instances? Where should I paste this data?

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";
    }
};
+3
3

, . yui

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

, YUI,

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

MyStaticVar, , , , , . , , , , go

Calendar.MyStaticVar

Augment YUI ,

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

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

- Javascript. .

, , . , , , o o, .

, .

(1) . , .

  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);  
  }

, y , : c1.y = 5555, c1, c2.

, , , ...

(2) Javascript, , getter setter.

  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());
  }
+2

, , . :

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
0
source

Source: https://habr.com/ru/post/1726054/


All Articles