tags:

views:

39

answers:

4

I have this inside the HTML that is creating an object in JavaScript

var myObject = new MyClass();

I know that after this I can refer to myObject and use it.

The problem is that I have the instantiation done in an anonymous way e.g.

new MyClass()

Is there a way to find then the instance so I can reuse it later in the code ? Any idea appreciated.

+2  A: 

It depends of how MyClass works...

It could have saved all the instances in a class property, but in most cases, you must assign the result of the instantiation to a variable...

Golmote
A: 

can you give your code?

Akie
+3  A: 

for example

lastOfMe = null

function myClass() {
   lastOfMe = this;
   this.x = 123;
}

new myClass();
alert(lastOfMe.x)

this is totally ugly though

stereofrog
+1 for "this is totally ugly though"
ormuriauga
Managed to implement this one with and working fine. thx.
dawez
+1  A: 

Anonymous objects are just that. If you want to use your object again, why create it anonymously?

If you really must do this, then just ensure that your object will register itself somewhere, like as a property of a global or similar, so you can grab it later.

mkoistinen