This is what I’m attempting to do, but I’m getting an error that I can’t bind to undefined, I assume because I’m in an anonymous function. I need to access the method (getAndSayHi
) the AJAX call is in.
JavaScript
x
13
13
1
var Parent() = new Function () {
2
this.sayHi = function (name) {
3
console.log("hello " + name);
4
}
5
this.getAndSayHi = function () {
6
$.ajax({
7
.
8
success: function(data) {
9
this.sayHi.bind(this, data);
10
}
11
});
12
}
13
How can I achieve this?
Advertisement
Answer
Try
JavaScript
1
10
10
1
this.getAndSayHi = function () {
2
var parent = this;
3
$.ajax({
4
.
5
success: function(data) {
6
parent.sayHi.bind(this, data);
7
}
8
});
9
}
10