Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Object.call" mean?

Tags:

javascript

oop

function bb_graphics_GraphicsContext(){
    Object.call(this);
    this.bbdevice=null;
    this.bbmatrixSp=0;
    this.bbix=1.000000;
    this.bbiy=0;
    this.bbjx=0;
    this.bbjy=1.000000;
    this.bbtx=0;
    this.bbty=0;
    this.bbtformed=0;
    this.bbmatDirty=0;
    this.bbcolor_r=0;
    this.bbcolor_g=0;
    this.bbcolor_b=0;
    this.bbalpha=0;
    this.bbblend=0;
    this.bbscissor_x=0;
    this.bbscissor_y=0;
    this.bbscissor_width=0;
    this.bbscissor_height=0;
    this.bbmatrixStack=new_number_array(192);
}

What does Object.call(this) mean?

like image 932
DrStrangeLove Avatar asked Apr 01 '12 12:04

DrStrangeLove


People also ask

What does calling object mean?

"calling object" means the object that is calling method . "called object" means the object on which method acts.

How do you call an object?

Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '. ' (period), and provide any arguments to the method within enclosing parentheses.

How do you call an object in a function?

The call() allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method. With call() , you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.

What is method call in oop?

Method Calls A method is a routine that applies to a particular class of objects. Once an object is declared, you can refer to it by its identifier when calling methods. The following example calls the SetActive method on the Find dialog box: Find.SetActive ()


1 Answers

Functions in JavaScript are full-fledged objects. They also, when passed as an argument to another function, don't retain their scope. So, in the following code...

var obj1 = {
    property1: "blah",
    method1: function () {
        alert(this.property1);
        // do stuff
    }
 };

 function func1 (passedFunction) {
     passedFunction();
     // do other stuff
 }

 func1(obj1.method1);

... func1 will call obj1.method1, but it won't alert the value of obj1's property1, because all we've done is pass the function object, not its this context. That's where call and apply come in. They allow you to inject scope, tell the function what the meaning of this will be. The following example works:

var obj1 = {
    property1: "blah",
    method1: function () {
        alert(this.property1);
        // do stuff
    }
 };

 function func1 (passedObject, passedFunction) {
     passedFunction.call(passedObject);
     // do other stuff
 }

 func1(ob1, obj1.method1);

Now, we've forced or explicitly told obj1.method1 what its context will by invoking call, and passing it the object it's to use as this.

call and apply are almost identical, except for how they handle additional arguments to the function being invoked. See these articles on MDN for more information: call, apply and Function.

All of this having been said, bb_graphics_GraphicsContext is a constructor. (Which you've probably guessed.) You invoke it by using the new keyword, var obj1 = new bb_graphics_GraphicsContext();. When it reaches line 1 of the function, it takes the this object, and calls the generic Object constructor, explicitly injecting the new object this (in the bb_graphics_GraphicsContext constructor) as the this of the Object constructor. I'd assume the writer of this function/constructor was doing this to make sure that the newly created object in bb_graphics_GraphicsContext was getting all the base methods of the base Object. But I don't know why this would be necessary, as if you call bb_graphics_GraphicsContext with the new keyword it will grab all these properties naturally.

like image 167
Paul Bruno Avatar answered Oct 14 '22 01:10

Paul Bruno