0
Why this code isn't working?
Could anyone please tell me why this code isn't working? This return NaN and I don't know why. Array.prototype.exe = () => { return (Math.max(this)); } let arr = [3,9,6]; console.log(arr.exe()); Many thanks
2 Respostas
+ 3
'this' works differently in arrow functions. Arrow functions refer to the scope they were declared so this is the global window object. Using the good old function operator solves this.
Also you need to spread the array using ...this because Math.max uses individual arguments instead of whole arrays
https://code.sololearn.com/WzPeHfAq2YMM/?ref=app
+ 1
Thank you very much. Your explanation really helped me.