Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Array as Object #17

Open
7flash opened this issue Mar 22, 2018 · 6 comments
Open

Array as Object #17

7flash opened this issue Mar 22, 2018 · 6 comments

Comments

@7flash
Copy link

7flash commented Mar 22, 2018

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

What happens in this case if proposal would be accepted?

@ljharb
Copy link
Member

ljharb commented Mar 22, 2018

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.

@7flash
Copy link
Author

7flash commented Mar 22, 2018

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

@keithamus
Copy link
Member

keithamus commented Mar 22, 2018

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.

@ljharb
Copy link
Member

ljharb commented Mar 22, 2018

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.

@keithamus
Copy link
Member

keithamus commented Mar 22, 2018

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.

@jonasraoni
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants