Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'this' keyword Refer to in this Method

Tags:

javascript

I'm just a bit confused about how the this keyword is used in this context. It is placed in an anonymous function with the parameter callback and is then used as such: callback(this[i], i, this). The exercise doesn't go into depth, but I understand that the this is referring to the ar object that is in the __proto__. Why are 3 arguments being placed in the anonymous function's parameter callback(this[i],i,this)and how is it working under the hood? Any insight would be greatly appreciated, thank you.

Just to add to what was previously said, the exercise asked for me to implement my own version of Array.prototype.map.

Array.prototype.map = function(callback) {
  let arr = [];
  for(let i = 0; i < this.length; i++){
    arr.push(callback(this[i], i , this))
  }

  return arr;
}

let  ar = new Array()
like image 782
Brian Aquino Avatar asked Apr 10 '19 05:04

Brian Aquino


People also ask

What is the this reference in Java?

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this .

Can we use this () in a method?

this() can be used to invoke current class constructor. this can be passed as an argument in the method call. this can be passed as argument in the constructor call. this can be used to return the current class instance from the method.

What is this keyword in selenium?

Its illegal in Java to declare the two local variables with the same name inside the same enclosing scopes (same enclosing scopes means inside the same class, same method etc).


1 Answers

The map function will be called on an Array instance, so in the following lines of code:

for(let i = 0; i < this.length; i++){
    arr.push(callback(this[i], i , this))
}

is as per the signature of the callback function passed to the Array.prototype.map method:

callback Function that produces an element of the new Array, taking three arguments:

currentValue The current element being processed in the array.

index Optional The index of the current element being processed in the array.

array Optional The array map was called upon.

So to breakdown the the three arguments in the callback function in your snippet:

this[i] translates to the first parameter of the map callback, which is the currentValue. As you understood correctly this is the array instance on which the map is called. So this[i] will refer to the value in the ith index of the array

i is the index or current index. This is the ith index of the iteration of the for loop being sent to the callback.

this is an reference to the array itself on which the map is invoked.

const arr = [1, 2, 3];
const callback = function(currentElement, index, array){
  console.log(`The current element ${currentElement}`);
  console.log(`The current index ${index}`);
  console.log(`The array instance ${array}`);
  return currentElement * 2; //multiplies each element with 2, this is the mapping operation
}
const mappedArray = arr.map(callback);
console.log(`The mapped array ${mappedArray}`);
like image 76
Fullstack Guy Avatar answered Oct 19 '22 19:10

Fullstack Guy