View Full Version : nested functions
bimal
04-01-2003, 06:57 PM
how do u access an instance variable of a class from inside a nested function. for example,
function HelloClass () {
this.x = "HELLO";
}
HelloClass.prototype.update()
{
hXML = new XML();
hXML.onLoad = function(success)
{
if(success)
{
trace( /*this.x : how do i put an instance variable in here */ + this.toString());
}
else
{
//error
}
}
hXML.load("hello.xml");
}
Jesse
04-01-2003, 10:34 PM
I'm not sure exactly what you mean. If you're trying to access a Class level variable from an instance of that Class:
function Foo() {
this.msg = "I am not a crook";
}
Foo.prototype.bar = function() {
trace(this.msg);
};
myFoo = new Foo();
myFoo.bar();
bimal
04-01-2003, 11:47 PM
i understand that...
however is it possible for a nested function inside a class method to access a class variable?
or is the scope of the nested function is limited to the class method it is defined in?
thanks
Jesse
04-03-2003, 05:18 AM
Hmm I see your point. For the moment I cna't get it working either but one thing to consider is that in order to be able to acces it you need to define your XMLM element as a child of the instance of the class you're using, otherwise the hXML object will end up on the _root level. This code still doesn'tw ork but might help you a bit:
function HelloClass() {
this.x = "HELLO";
}
HelloClass.prototype = Object.prototype
HelloClass.prototype.myupdate = function() {
this.hXML = new XML();
this.hXML.onLoad = function(success) {
if (success) {
trace(this._parent);
} else {
//error
}
};
this.hXML.load('mytest.xml');
};
myObject = new HelloClass();
myObject.myupdate();
hi guys,
the XML object seems not to have a reference to its _parent, so you need to attach it yourself.
i named the reference to the parent '_parent'.
maybe it's better to give it another name, that is no keyword in AS:
function HelloClass() {
this.x = " HELLO ";
}
//
HelloClass.prototype.myupdate = function() {
// works with 'normal' functions
this.a = function() {
trace(this.x);
};
this.a();
// special case XML, got no _parent on default, needs to be attached 'by hand'
this.hXML = new XML();
this.hXML._parent = this; // maybe better to use another property name than '_parent'
this.hXML.onLoad = function(success) {
if (success) {
trace(this._parent.x);
}
else {
//error
}
};
this.hXML.load('test.xml');
};
myObject = new HelloClass();
myObject.myupdate();
hope it helps.
i++
bimal
04-03-2003, 01:02 PM
good one there... thanks a lot... tht helps
Jesse
04-03-2003, 08:34 PM
Great work i++. Thx.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.