What does "Object.call" mean?

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?

+7
source share
5 answers

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); // do stuff } }; function func1 (passedFunction) { passedFunction(); // do other stuff } func1(obj1.method1); 

... 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); // do stuff } }; function func1 (passedObject, passedFunction) { passedFunction.call(passedObject); // do other stuff } func1(ob1, obj1.method1); 

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.

+9
source

Object.call will execute a specific function in the provided context, it can be used to call functions from one object to another.

The mozilla dev network gives a very good explanation

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call

0
source

This will do absolutely nothing except loss of resources and memory allocation.

If Object.call (this) is assigned to a variable or property of the function constructor bb_graphics_GraphicsContext

this.myObject = Object.call (this)

The only thing you get in this case is the empty object "THAT DOES NOT DO THE PROVISED CONTEXT"

 function MyConstructor(){ this.test01 = 0; var b = Object.call(this); // similar to b = {}; or b = new Object() console.log(b); // log object console.log(b.test); // log undefined this.test = 1; } var myObject = new MyConstructor(); console.log(myObject, window.test01) 
0
source

Although Object.call is likely to do nothing, as stated here, the concept may be important. Basically, the example that you will see by inheritance in the Node.js documentation :

 const util = require('util'); const EventEmitter = require('events'); function MyStream() { EventEmitter.call(this); } util.inherits(MyStream, EventEmitter); 

util.inherits will create a new MyStream inherits (has the same prototype as) the EventEmmiter . This may be enough if we are interested in MyStream having access to the functions inherited by the prototype EventEmmiter . But what if variables are passed in by design? What to do if we have:

 function MyObject() { this.code = "2nV_ahR"; } 

In this case, the code variable is passed at runtime when MyObject receives the instance. Therefore, the subclass must pass:

 function MySubObject() { MyObject.call(this); } 

To inherit the code variable. That call accepts a parameter that sets the this variable. So ... when I do var o = new MySubObject() , this inside MySubObject refers to o , which is then passed to the call method, so when MyObject does this.code = ... this is actually passing code to o !

0
source

Each JavaScript function has toString() , call() and apply() .

You can learn more about them in this article odetocode.com

-one
source

All Articles