[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Class with internal callback referencing itself.

puritanpaul

7/29/2015 7:51:00 AM

Can someone offer a way around this problem?

MyClass.prototype.selectClinic = function() {
this.otherClass.doSomething( this.selectClinicSuccess );
}

MyClass.prototype.selectClinicSuccess = function() {
this.getProperty()
}

MyClass.prototype.getProperty = function() {
// do some stuff
}

When I land in selectClinicSuccess, I'm getting this.getProperty is not a function.

Thanks
3 Answers

Evertjan.

7/29/2015 8:14:00 AM

0

puritanpaul@gmail.com wrote on 29 Jul 2015 in comp.lang.javascript:

> Can someone offer a way around this problem?
>
> MyClass.prototype.selectClinic = function() {
> this.otherClass.doSomething( this.selectClinicSuccess );
>}
>
> MyClass.prototype.selectClinicSuccess = function() {
> this.getProperty()
>}
>
> MyClass.prototype.getProperty = function() {
> // do some stuff
>}
>
> When I land in selectClinicSuccess, I'm getting this.getProperty is not
> a function.

Could it be you are making an unapproved landing
on a Javascript runway not designed for getProperty()?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Ben Bacarisse

7/29/2015 9:50:00 AM

0

puritanpaul@gmail.com writes:

> Can someone offer a way around this problem?
>
> MyClass.prototype.selectClinic = function() {
> this.otherClass.doSomething( this.selectClinicSuccess );
> }
>
> MyClass.prototype.selectClinicSuccess = function() {
> this.getProperty()
> }
>
> MyClass.prototype.getProperty = function() {
> // do some stuff
> }
>
> When I land in selectClinicSuccess, I'm getting this.getProperty is
> not a function.

You don't show enough code to be sure, but passing
this.selectClinicSuccess to doSomething is almost certainly wrong.
doSomething probably call calls its argument, in which case 'this' will
be undefined inside selectClinicSuccess. You might want

this.otherClass.doSomething( this.selectClinicSuccess.bind(this) );

--
Ben.

puritanpaul

7/29/2015 6:48:00 PM

0

Perfect! I didn't know about function binding, but it makes a lot of sense. Thanks for the help.


> You don't show enough code to be sure, but passing
> this.selectClinicSuccess to doSomething is almost certainly wrong.
> doSomething probably call calls its argument, in which case 'this' will
> be undefined inside selectClinicSuccess. You might want
>
> this.otherClass.doSomething( this.selectClinicSuccess.bind(this) );
>
> --
> Ben.