-1

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 prototype.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

2
  • How would you do it in any other OO language? Commented Jan 14, 2011 at 14:36
  • Hmmyeah... i started to think about that too... and then came to solution i dont need to do it at all :P thanks to everyone who answered :) Commented Jan 14, 2011 at 14:45

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.