problem with OOP Javascript
I have one javasctipc class... Lets call it X.
myClass = new X(bunc, of, stuff);
Then i have method like:
X.prototype.drawTripOnMap = function(request) {
...
var y = new ChartClass();
//or this.chart = new ChartClass():
y.drawchart(data, options, etc);
...
}
Now i dont want to create new instance of X - How can i, within ChartClass.drawchart method call myClass methods?
to give you bit more info about my problem - im writing pure javascript class which handles bunch of OpenLayer stuff. My website uses 开发者_如何学Pythonprototype.js atm, but we want to get rid of that. We cant do it right away, so im tryng to write my class so that i could easyly swap out parts that handle dom, events or ajax calls. right now im drawin charts below the map and i need to handle chart click events. When i click on chart, something needs to happen on map... vice versa is simple - since chart can be subclass of myClass (this.chart in above code).
soo... how?
Alan
Consider passing instanceof X "class" to the ChartClass constructor.
var y = new ChartClass(this);
Having stored the reference to this passed (it's your myClass object) you may call its methods from within ChartClass later on.
You can call prototype methods directly without requiring an instance, and specify the scope using:
X.prototype.drawTripOnMap.apply(null, [request]);
But I would question why you have a class method that doesn't require the context of an instantiated object, why not just make it a global function?
The way you have it written now, myClass
is a global (since there isn't a var) so you can just do
myClass.someMethod
This is just a guess. I can provide more help, but I'm not sure what the issue really is. Maybe your response to this will help guide me to providing a better answer.
精彩评论