I have some objected oriented JS that works fine in firefox but not in IE 8 (though it will be IE 9 that we will need to support).
When I do:
"self = this;"
IE flags that as an error.
I am trying to set this to self to then use it in a jquery callback to call some other
method in my JS object.
this.upd_params = function () {
$("#add-parameter-modal").modal('hide');
var param_form = $('#add_param_form');
self = this;
this.added_params = [];
this.removed_params = [];
$('.unused_parameter').each(function (index, obj) {
if (obj.checked) {
id = self.get_idnum(obj.id);
self.add_param2list(id);
}
});
$('.used_parameter').each(function (index, obj) {
if (!obj.checked) {
id = self.get_idnum(obj.id);
self.remove_param(id);
}
});
this.upd_html();
cfg_form_changed = true;
};
The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.
You might sometimes use self to capture the context of this before it is destroyed by some function. Unfortunately self is also an alias for window , the global top-level object.
In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword.
In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.
Make sure self
is a locally-scoped (not global) variable.
var self = this;
Otherwise, self
refers to window.self
and assigning to it is not allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With