Functions in JavaScript are complete objects. They also, when they are passed as an argument to another function, do not retain their scale. So in the following code ...
var obj1 = { property1: "blah", method1: function () { alert(this.property1);
... func1 will call obj1.method1 , but there will not be an alert value obj1 property1 , because all we did was pass the object to the function, not this context. That includes call and apply . They allow you to enter a scope, tell functions what this value will mean. The following example works:
var obj1 = { property1: "blah", method1: function () { alert(this.property1);
Now we have forced or explicitly told obj1.method1 that its context will call , and pass it an object that it will use as this .
call and apply almost identical, except how they handle the extra arguments of the function being called. See These MDN Articles for more information: call , apply, and Function .
All of the above, bb_graphics_GraphicsContext is a constructor. (Probably you guessed it.) You call it with the keyword new , var obj1 = new bb_graphics_GraphicsContext(); . When it reaches line 1 of the function, it takes a this object and calls the general Object constructor, explicitly introducing a new this object (in the bb_graphics_GraphicsContext constructor) as this in the Object constructor. I assume that the author of this function / constructor did this to make sure that the newly created object in bb_graphics_GraphicsContext received all the basic methods of the Object base. But I donβt know why it would be necessary, as if you call bb_graphics_GraphicsContext with the new keyword, it will definitely get all these properties.
Paul Sebastian Bruno
source share