PDA

View Full Version : how to access instantianted object correctly?



digitalkaoz
13 Jun 2007, 3:40 AM
i have some problem in getting instantiated objects.

here is my code shorthand


//create Jame and Jame.widgets

Jame.widgets.Message = function(_title){ //message object
this.title = _title;
alert(test.title); //doesnt work, and i dont know why
}
Jame.widgets.Login = function(_title){ //login object
this.title = _title;
}

test = new Jame.widgets.Login("test"); //instantiate login first
test2= new Jame.widgets.Message("dsfs"); //instantiate message


in the Message Object i cant access the instantiated "test" Object (login), what am i doing wrong?
i would be thankful for any hints...

greetz digitalkaoz

Animal
13 Jun 2007, 3:48 AM
"test" does not exist. You mean "this". You can't, in a constructor, refer to any old variable name that might or might not exist!

I mean why not write



alert(fooBarBletchVariableName.title);


It stands just as good a chance of existing!

Animal
13 Jun 2007, 3:50 AM
Take a look at this thread: http://extjs.com/forum/showthread.php?t=441

digitalkaoz
13 Jun 2007, 3:51 AM
ok but what if there are existing more then 1 MessageWidget? how do i access and/or store them correctly, so i can access there attributes at any time?

mh ok the objects are needing a return{} function? the public interface?

Eric Suen
13 Jun 2007, 4:16 AM
Hmm, it works on my computer, both IE and firefox...

liggett78
13 Jun 2007, 4:25 AM
ok but what if there are existing more then 1 MessageWidget? how do i access and/or store them correctly, so i can access there attributes at any time?

mh ok the objects are needing a return{} function? the public interface?

You just add methods to the prototype, like Message.prototype.someFunction = function()... and use this inside the function to refer to the current instance with all data specific to it.

digitalkaoz
13 Jun 2007, 5:09 AM
You just add methods to the prototype, like Message.prototype.someFunction = function()... and use this inside the function to refer to the current instance with all data specific to it.

worked for me thanks...

http://phrogz.net/JS/Classes/OOPinJS.html maked it a bit clearer to me ;)