+ 1

A question

Array.prototype.func = () => return (this instance of Array); alert((new Array()).func()); Why the output is false? Does the __proto__ of new object not point to prototype of Array? I have also tried to use "this.constructor==Array". But it doesn't work too. Does the constructor of prototype not point to Array class?

22nd Sep 2018, 8:29 PM
Yusuf
Yusuf - avatar
3 odpowiedzi
+ 3
just ditch the arrow function and go back to the regular function: Array.prototype.func = function() { return (this instanceof Array); }; alert((new Array()).func()); more about arrow func: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
23rd Sep 2018, 12:09 AM
MO ELomari
MO ELomari - avatar
+ 2
this points to the global object in arrow function.
27th Jun 2019, 10:37 PM
Yusuf
Yusuf - avatar
+ 1
An arrow functions doesn't have it's own this, thus rendering it not viable to be a method. In your code, this points to the Window object, which is not an instance of Array. Make a function declaration like the other languages.
23rd Sep 2018, 12:21 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar