access singleton and inner classes each other in javascript -
i'm kind of new javascript. i'm confused javascript objects!!
my code skeleton bellow...
var jcanvas = new function(){ this.init = function(canvasid){ ... }; var drawingmanager = new function(){ drawinfos = []; // drawinfo objects pushed this.mousestate = mousestate.released; ... }; function drawinfo(bm, cl, id, x, y){ ... } function point(x, y){ ... } var mousestate = new function(){ ... }; var color = new function(){ ... }; var brushmode = new function(){ ... }; };
- i want jcanvas singleton class object.
- in jcanvas object, there many singleton classes such drawingmanager, mousestate, color, brushmode. , 2 more classes not singleton classes(point, drawinfo)
- what want that, in drawingmanager, want access other classes , singleton class objects.
problem browser gives error "mousestate undefined".
i think i'm familiar java, c# etc... want program have structure javascript make me confused , don't know how make design pattern..
please me out..
to declare functions, don't use new
keyword. use when creating instances of objects.
in javascript, can declare "class" (the body of function constructor):
function myclass (arg1) { this.myclassproperty = arg1; }
and instantiate it:
var myobj = new myclass();
if want create singleton, best method use invoked function:
var mysingleton = (function() { var myprivatefunction = function() { ... }; var myexportedfunction = function() { ... }; var myprivatevar = 5; return { myexportedfunction: myexportedfunction }; })();
here, create anonymous function , run it. kind of more advanced concept though. or can create object:
var mysingleton = { myproperty: 1, myfunction: function() { ... }, ... };
Comments
Post a Comment