javascript - Why should "this" be assigned to variable in jQuery? -
In some jQuery code I often think that this
is assigned to the variable in jQuery and I do not understand the reason for this approach?
This is an example of a jQuery plugin builder:
// constructor function Nullpoll (element, option) {var widget = this; Widget.config = $ .extend ({}, default, options); Widget.element = element; Widget.element.on ("click", function () {alert ("test")}; Widget.element.one ("change", function (e) {widget.element.find ("button"). RemoveProp ("disabled");}); This.init (); }
The value of this
is determined by how <
- or
- (more commonly) to give it a more informative name so that value one different Function , which can reach the scope of the first function
In this particular case, widget.element.one ()
Near anonymously function call this
call to call Nullpoll
. If it tries to use the this
keyword, then it will get a different value (how the value depends on whether a a
function is called a mismatch function) < / P>
Comments
Post a Comment