javascript - Can't access object method -


i've created quasi namespace/class called newscalendar. intent avoid polluting global namespace hide functions should private. when try call should public method "object not support property or method."

function newscalendar() {     var privatevar; //private vars      this.onload = function () //public method     {         //do stuff     };      function getweekstart(date) //private function     {         //do stuff     } } 

the comments there indicate how i'm intending each item behave.

i tried calling onload function this:

newscalendar.onload(); 

but causes "object not support property or method" error. want able use newscalendar namespace , able call variables/methods attached object using newscalendar.propertyname or newscalendar.methodname().

try following setup:

var newscalendar = (function () {     var privatevar;      var onloadhandler = function () {         //do stuff     };      function getweekstart(date) {         //do stuff     }      return {         onload: onloadhandler     }; })(); 

and use:

newscalendar.onload(); 

anything in returned {} public. inside of surrounding function accessible inside of it.

the reason suggest setup because don't seem want instantitate several objects, instead use library of properties/methods.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -