2

I am reading the book JavaScript: The Good Parts. It is said that

Objects in JavaScript are mutable keyed collections.

What does mutable keyed collection mean?

AS far as I could find on internet, mutable means the vales can be changes. I couldn't find what keyed collection mean.

1

3 Answers 3

1

Objects are a collection of keys with associated values. This could be referred to as a "keyed collection":

var o = {
  foo: "bar",
  bar: "baz"
}

(Where foo and bar here are keys).

...which can be changed (as you've already said, the "mutable" part):

o.foo = "foobar";
o.foobar = "bar";
Sign up to request clarification or add additional context in comments.

15 Comments

Is a method's name also a key?
It can be, yes. If we define o.foo = function() { ... }, "foo" here is the key.
@JamesDonnelly, Can you explain All types except objects define immutable values (values, which are incapable of being changed) Ref.. If possible..
@RayonDabre I'm not sure what you're asking.
Doc says anything except Object are immutable(can not be changed) bit we do can change the values of String/Number..
|
1

The keyed keyword here means that the data is "named", "indexed" or "keyed".

{ 
 key : value,
 key2: value2 
}

a collection because it contains a collection of data.

Comments

0

It is about the way objects work in Javascript, they behave like C# Dictionaries, for example, or named arrays in PHP. obj.someKey is equivalent to obj['someKey'] and you can always change the values associated with those keys or indeed delete them.

More advanced: the key uniquely identifies the value stored with it and the system is optimized for performance, so you can use this to index information or to get distinct values of a list,etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.