proposal-array-last icon indicating copy to clipboard operation
proposal-array-last copied to clipboard

Array as Object

Open 7flash opened this issue 6 years ago • 6 comments

let arr = []
arr.type = "animals"
arr.lastItem = "kitty"
console.log(arr.lastItem) // "kitty"

What happens in this case if proposal would be accepted?

7flash avatar Mar 22 '18 14:03 7flash

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.

ljharb avatar Mar 22 '18 15:03 ljharb

Do you mean that current behavior will not be changed? arr.lastItem still would be equal "kitty"?

7flash avatar Mar 22 '18 15:03 7flash

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.

keithamus avatar Mar 22 '18 15:03 keithamus

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.

ljharb avatar Mar 22 '18 15:03 ljharb

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.

keithamus avatar Mar 22 '18 15:03 keithamus

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.

jonasraoni avatar Apr 04 '18 12:04 jonasraoni