Throughout my coding journey, I've often heard the saying "Classes are just syntactic sugar for prototypes."
However, an interesting example challenges this notion.
function SubArray() {}
SubArray.prototype = new Array( );
console.log(Array.isArray(new SubArray())) // false
In contrast, consider the same example using classes.
SubArray = class extends Array{}
console.log(Array.isArray(new SubArray())) // true
Surprisingly, instanceof
works correctly with both new SubArray instanceof Array
.
But why does Array.isArray
fail to return true
with prototypes in this case?