3. Objects: things with named parts
Arrays number their values; objects name them. Put objects inside an array and you have the shape at the heart of this whole book.
- Create an object, and read, change and add properties with dot and bracket access.
- Explain when you need brackets (a computed key) instead of a dot.
- Store records in an array of objects and loop over them.
- Describe the array-of-structs (AoS) layout and how a struct of arrays (SoA) differs.
Values that belong together
A particle in a game has a position and a velocity: four numbers. You
could keep them in an array, [3, 4, 1, 2], but then you have to
remember that slot 2 means “horizontal speed”. Six months later, nobody
remembers.
An object is a filled-in form. Each line of the form has a label (“x”, “y”, “speed”) and a value written next to it. You look things up by label, not by position, so the meaning is right there in the code.
const p = { x: 3, y: 4, vx: 1, vy: 2 };
console.log(p.x); // 3
console.log(p.vx); // 1
Curly braces with key: value pairs make an object. A dot followed by a
name reads one property.
An object is a collection of properties. Each property is a
key (a name, which is a string) paired with a value. obj.key
reads or writes the property called key. This is dot access.
Change it, grow it
Properties can be changed and added at any time:
const p = { x: 3, y: 4 };
p.x = 10; // change
p.color = 'red'; // add a new one
console.log(p);
// { x: 10, y: 4, color: 'red' }
console.log(p.size); // undefined
Reading a property that doesn’t exist gives undefined, just like reading
past the end of an array.
Brackets: when the name is in a variable
Sometimes you don’t know which property you want until the program runs. Maybe the user picks “x” or “y”. Dot access needs the name written literally in the code, so there’s a second way:
const p = { x: 3, y: 4 };
const key = 'y';
console.log(p['x']); // 3
console.log(p[key]); // 4 (looks up 'y')
console.log(p.key); // undefined!
That last line is a classic bug. p.key means “the property literally
named key”, and there isn’t one. p[key] means “evaluate the variable
key first, then look up whatever string it holds”.
Bracket access obj[expr] works out expr first and uses the result
as the key. Use it when the key is computed at run time. obj.name and
obj['name'] mean the same thing.
const p = { x: 3, y: 4 };
p.x = 10;
p.color = 'red';
console.log(p);
const key = 'y';
console.log(p[key]);
console.log(p.key);
console.log(p.size);Arrays of objects
Now combine this chapter with the last one. A game doesn’t have one particle, it has thousands. So you put them in an array:
const particles = [
{ x: 0, y: 0, vx: 1, vy: 2 },
{ x: 5, y: 5, vx: -1, vy: 0 },
{ x: 9, y: 1, vx: 0, vy: 3 },
];
for (let i = 0; i < particles.length; i++) {
const p = particles[i];
p.x = p.x + p.vx;
p.y = p.y + p.vy;
}
particles[i] is an object, so particles[i].x (or p.x after
const p = particles[i]) reaches inside it. This is the most natural way
to write it, and it has a name you’ll see all through this book: an array
of structs, or AoS. (“Struct” is the old C word for a record with
named fields.)
Here’s the question this book is built around. The loop above only
touches x, y, vx and vy. What if each particle also had a
mass, a color, a name and a health, none of which the loop needs?
Is it a problem that they’re all packed in alongside?
Turn the picture sideways. Instead of one object per particle, keep one
array per property: all the x values together, then all the y
values, and so on. That is a struct of arrays, or SoA:
Same data, different arrangement. In Part II you’ll learn why the arrangement can make a loop several times faster or slower, and you’ll measure it yourself. Researchers have even built JavaScript runtimes that switch an array of objects to columns automatically while the program runs[3]. Viscosity’s job will be to make that choice, safely, for you.
Engines notice when many objects have the same properties, added in the
same order, and give them a shared hidden description called a shape
(V8 calls it a map, or hidden class). Then reading p.x doesn’t have to
search for “x” every time: the engine already knows where x lives in
every object of that shape[2]. Part V opens this
up properly.
Your turn
Write centerX(ps) that returns the average of the x properties of all the particle objects in the array ps. You can assume the array is not empty.
function centerX(ps) {
// your code here
}
const ps = [
{ x: 2 },
{ x: 4 },
{ x: 9 },
];
console.log(centerX(ps));Write step(ps) that moves every particle by its velocity: x grows by vx, and y grows by vy. Change the objects themselves; the function doesn’t need to return anything.
function step(ps) {
// your code here
}
const ps = [
{ x: 0, y: 0, vx: 1, vy: 2 },
];
step(ps);
console.log(ps[0]);What you learned
- An object stores properties: named keys with values.
obj.keyis dot access.obj[expr]is bracket access, which computes the key at run time.- Missing properties read as
undefined. New ones can be added at any time. - An array of objects is the array of structs (AoS) layout: each slot holds a whole record. A struct of arrays (SoA) keeps one array per property instead.
Next comes the chapter that explains what a variable really holds when it holds an object. It’s the key to the hardest problem Viscosity has to solve.
Check your understanding
0/6- 1.
const p = { x: 1, y: 2 };What isp.z? - 2.
What does this print?
const p = { a: 1, b: 2 }; const k = 'b'; console.log(p[k]); - 3.
Which line is a common bug when the key is held in a variable
key? - 4.
In an array of particle objects, which expression is the
vyof the first particle? - 5.
What is the difference between AoS and SoA?
- 6.
Why do engines like V8 give many objects a shared hidden “shape”?
References
- [1] MDN contributors. Working with objects (JavaScript Guide). MDN Web Docs. developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_objects (checked 2026-10-31)
- [2] Mathias Bynens, Benedikt Meurer (2018). JavaScript engine fundamentals: Shapes and Inline Caches. mathiasbynens.be. mathiasbynens.be/notes/shapes-ics (checked 2026-10-31)
- [3] Lukas Makor, Sebastian Kloibhofer, David Leopoldseder, Daniele Bonetta, Lukas Stadler, Hanspeter Mössenböck (2022). Automatic Array Transformation to Columnar Storage at Run Time. Proceedings of the 19th International Conference on Managed Programming Languages and Runtimes (MPLR ’22), ACM, pp. 16–28. doi:10.1145/3546918.3546919 (checked 2026-10-31)