proposal-array-last
proposal-array-last copied to clipboard
Array as Object
let arr = []
arr.type = "animals"
arr.lastItem = "kitty"
console.log(arr.lastItem) // "kitty"
What happens in this case if proposal would be accepted?
The same thing that happens if you do arr.map = function () { return 3; }
; it shadows the prototype method/accessor. This is a normal part of JS.
Do you mean that current behavior will not be changed? arr.lastItem still would be equal "kitty"?
The spec proposal does not currently reflect this, calling arr.lastItem = "kitty"
would throw. We could amend the spec proposal to not throw - so arr.lastItem = "kitty"
would set the lastItem
property. I'm undecided if we should do this.
ah, I'd misunderstood.
Yes, I think that either it should throw or it should define an own property, but the latter might be needed for web compat.
Agreed. I'd rather throw but webcompat would probably force otherwise. However lastItem
as a getter/setter may be dropped given concerns from the March TC39 meeting - instead opting for a method that can be overwritten ad-hoc.
It shouldn't throw, shadowing and overwriting are great features of the language, I would say they represent better the real world, where everything is dynamic. Although we have awkward differences, like being able to break the Set (e.g. delete Set.prototype.size), but blocked from doing so with the length property of an Array/String.