I am having trouble understanding this code:
$.functionone = function(){
function setOptions(newOptions){
...
}
this.setOptions = setOptions;
}
what does adding ‘this’ in ‘this.setOptions’ for? I understand that its referencing the function setOptions, but does adding the ‘this’ there make the function get called? I know this refers to the DOM element, but whats the point of having it in this particular scenario. Thanks.
That will simply expose the function from the scope of
functionone, to be a property of the$object.For example:
The
thisvalue on JavaScript is set implicitly when you make a function call.If the function is bound as a property of an object (like
$.functionone), thethisvalue will refer to the base object ($in your example).That’s not so useful IMO, it’s equivalent to:
Which is at the end, when you invoke
functionone, equivalent to:The difference is that the function is not named, which can be useful for debugging.
Working with the
thisvalue on jQuery plugins is more usual when you extend thejQuery.fnobject, in that case thethisvalue refers to the jQuery object that contains the matched elements, and not to the jQuery constructor itself.