In your example $.jqplot.Dragable
is a constructor. this
refers to the object created when
new $.jqplot.Dragable(options)
is called. As for $.extend
, It simply copies every method and property from options
onto this
.
But nevermind that. First you should understand what this
is. After that you can read the links I've posted at the bottom of my aswer to learn about constructors and OOP in JavaScript.
In javascript this
refers to the context of a method or function. The value of this
can be of three things.
Global Context
When simply executing a function by name or by a direct variable reference (not an object property), this
points to the global scope. In the browser this is the window
object.
Note that in strict mode this
is undefined
function setFoo(val) {
this.foo = val;
}
Above we have a function setFoo
. This function simply takes a value and sets it on this
. Lets execute the function to find out what will happen...
setFoo(10);
In this case, this
refers to the global scope. For this reason, window.foo
or foo
should equal 10.
window.foo == 10 //true
foo == 10 //true
Object Context
When a function is stored as a property of an object it is called a method. When a method is executed this
becomes the object the method is attached to. This allows the method to modify and use data on the object.
Now lets take setFoo and attach it to an object.
var myObject = {};
myObject.setFoo = setFoo;
If we execute with now myObject will have a new property called foo and it will equal what ever we pass into myObject.setFoo
.
myObject.setFoo(34);
As it should myObject.foo
equals 34 because this
refers to the object myObject
.
myObject.foo == 34; //true
Call Context
The last way this
can be set is with to methods of the Function
prototype. This means that all functions and methods in JavaScript have these to methods. They are call
and apply
. These functions allow the programmer to set a custom context while executing a function or method. The two are nearly identical except that apply takes an array as its second argument. Each item in the array is passed to the function/method as an argument in order.
call
var x = {};
setFoo.call(x, 20);
x.foo == 20; // true
apply
var x = {};
var args = [20];
setFoo.apply(x, args);
x.foo == 20; // true
You should read the article for this
on MDN. It will help you better understand when to use this, and why its an important part of function scope.
MDN - this
You should also read because the main purpose of this
is to enable OOP (Object Orientated Programming) in JavaScript.
MDN - Inheritance and the prototype chain