Quick question: Wouldn’t you be able to extend the prototype too to produce the same behaviour ?
Yes, you can. JS is wild. You can do pretty much anything you can think of.
@aminnairi shows a great example
Yes you could. Even though this is considered a bad practice to extend global objects for demonstration purpose you could add a property to the Array prototype.
Array
All future arrays will inherit this property after extension.
Array.prototype.greeting = "Hello world!"; const primes = [2, 3, 5, 7, 11]; const fruits = ["banana", "apple", "mango"]; console.log(primes.greeting); // "Hello world!" console.log(fruits.greeting); // "Hello world!" console.log(primes.length); // 5 console.log(fruits.length); // 3
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Quick question:
Wouldn’t you be able to extend the prototype too to produce the same behaviour ?
Yes, you can. JS is wild. You can do pretty much anything you can think of.
@aminnairi shows a great example
Yes you could. Even though this is considered a bad practice to extend global objects for demonstration purpose you could add a property to the
Array
prototype.All future arrays will inherit this property after extension.