Is there any difference between xfcall (x, ...) and xf (...)?

I am looking at code that looks like

this.f.call(this); 

Or in some other cases

 this.someObj.f.call(this.someObj); 

Is there any difference between them and

 this.f(); this.someObj.f(); 

Are there any circumstances under which the behavior will be different? (For example, the behavior is different if this or someObj is null or is not actually an object, or f is not really a function? I cannot think of a way, an exception, and the other is not, but maybe I missed something. ..)

EDIT: To clarify: yes, I know that .call can be used to indicate the value of this that the function sees, and can be useful in cases where you cannot use the obj.f() syntax because f not an obj property or you don’t know if it is). My question is not how .call works at all. My question is about this case, when I do not see the obvious reason for using .call , and not the object-property syntax.

+6
source share
1 answer

There is no difference between them.

If we check the language specification for regular function calls , and then Function.prototype.call , we see that they behave exactly the same.

Namely,

xfcall(x) does the following:

Returns the result of calling the internal method fun [[Call]], providing thisArg as this value, and argList as a list of arguments.

If a normal call:

Returns the result of calling the [[Call]] internal method in func, providing thisValue as this value and providing the argList list as argument values.

Where this has permission (specified in 11.2.3 7.bi in the specification).

All modern JavaScript implementations (yes, even modern IE8) respect this.

+1
source

Source: https://habr.com/ru/post/952883/


All Articles