[SOLVED] Having trouble with understanding the Method borrowing


i have a problem understanding this code or with Method Borrowing ->

   function func() {
      [].join.call(arguments)   //how does this works?

join only needs a separator, why we are passing the this context value?
and more interesting is that it work!

is there anybody to explain this to me?



The .call() method is used on functions, it behaves in a similar way to using () to call a function such as when you perform functionName(), however, it allows you to explicitly define what the this is inside of the function, so for example:

function functionName(arg) {
  console.log("this", this); // this {"foo": 1}
  console.log("arg", arg); // arg str

functionName.call({foo: 1}, "str");

Notice that above, the first argument to .call() defines what this is inside of your function, and then the second argument onwards defines what the arguments are to functionName.

We can use .call() on any function, including the ones provided to us by JavaScript. In your code, you’re using [].join which exposes (not calls) the join function of arrays, that is, it gives you access to the join function defined on the Array.prototype object. With that in mind, your code could be rewritten like so to highlight that [].join is used just to obtain the join function from the array prototype:

function func() {
  const joinFn = Array.prototype.join; // or: [].join;
func('a', 'b', 'c'); // "a,b,c"

Internally, the .join() method on arrays uses this inside of it (similar to how functionName() uses this inside of it) to determine what object/array needs its elements to be "joined" together. In your example, you are not calling .join directly on your empty array (ie: [].join()), but instead, are using the empty array to access the .join function, and then calling it with the arguments object by using join.call(arguments). When you do this, the this inside of the .join() method is set to the arguments object and the join method is called with no arguments, so the separator defaults to ,. As the arguments object is array-like (it has indexes and a length property like an array does), its elements can be joined together just like an array’s elements can be joined together.

You can see section of the ECMAScript specification for further details on how Array.prototype.join works internally and how it is made generic to handle array-like objects and not just arrays.

Answered By – Nick Parsons

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.