Tell us what’s happening:
how does null work here, I am not able to understand
Your code so far
var arr = [6, 89, 3, 45];
var maximus = Math.max.apply(null, arr); // returns 89
**Your browser information:**
User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36</code>.
**Challenge:** Use the Spread Operator to Evaluate Arrays In-Place
**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place
your code doesn’t match the challenge you are asking about
if you don’t want to ask about a specific challenge you can just create a topic directly on the forum, or delete all the stuff that is not part of your question
please change your first post to make more clear what you are asking about
The argument thisArg
present in many native functions and methods allows you to specify the context of this
for a callback or method execution.
In this particular case, since this
is never queries anywhere in Math.max, you need not to specify it and many programmers put null
just because they have to put something; you can actually put anywhere there and it won’t matter.
But it DOES matter in other cases where you MUST be specific about what this
means. I think using .bind
or an arrow function (when it comes to callbacks) is more common that specifying thisArg
.
The thisArg
in apply and call can be useful when you want to borrow methods from other prototypes and apply whatever transformation or consumption they perform to a different object (with or without a different prototype):
const person = {
name: 'John Doe',
introduce(prefix, suffix) {
console.log(`${prefix} I am ${this.name}. ${suffix}`);
}
}
person.introduce.apply(
{ name: 'Ironman' },
['Hello!', 'Nice to meet you!']
);
// Hello! I am Ironman, Nice to meet you!
Now, inside the method, this
changes because it is now bound to a different object.