I have a jquery class within a normal class in javascript. Is it possible to access variables in the scope of the parent class from a callback function in the jquery class?
A simple example of what I mean is shown below
var simpleClass = function () {
this.status = "pending";
this.target = jqueryObject;
this.updateStatus = function() {
this.target.fadeOut("fast",function () {
this.status = "complete"; //this needs to update the parent class
});
};
};
Now in the above example, the callback function tries to access the scope of the jquery object. is there any way to access the status variable in the parent class?
By setting "this" to a variable you can access easily. Like:
Use an Arrow Function
Normal function syntax
function(param1, param2) {}
Arrow function syntax
(param1, param2) => {}
Usage
Using an Arrow function within a ECMAScript 2015 Class
Described code works only in modern browsers.
try this:
you can also define your target object as private variable
You can mantain state using closure variables:
You set "this" to a variable in the parent function and then use it in the inner function.
Sorry m8. You have to nest the reference down into the objects like so:
notice the
var _root