Reference to an object from a callback function in jQuery -
I have the following status in a pseudo-class creator: I attach an element to a click event. When the event is triggered, I would like to refer to the object from the callback function where the event was set.
Code of the creator of pseudo class
function MyClass () {This.myClassAttribute = "A class attribute"; // here refers to 'this' object $ ("span"). Click (function () {// here `` `` `` `` `` `` `` `` `` `` `` `` ` }
How to reference objects without a global variable?
An anonymous function in javascript is able to reference all the variables present in the scope of the creation of the function. Since this
is re-assigned to the callback function, so you can create a local variable to store it before you file a callback.
function MyClass () {this.myClassAttribute = "A class attribute"; Var myClass = this; $ ("Span"). Click (function () {myClass.myClassAttribute = "Hello";}); }
Comments
Post a Comment