Objects
- Objects are used to store keyed collections of various data and more complex entities.
- An object can be created with figure brackets
{…}
with an optional list of properties. A property is a “key: value” pair, wherekey
is a string (also called a “property name”), andvalue
can be anything. - An empty object can be created using one of two syntaxes:
let user = new Object(); // "object constructor" syntax let user = {}; // "object literal" syntax
-
Literals and properties
- Example:
let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };
- Property values are accessible using the dot notation:
// get property values of the object: alert( user.name ); // John alert( user.age ); // 30
- The value can be of any type (including boolean).
- To remove a property:
delete user.age;
- We can also use multiword property names, but then they must be quoted.
- Rule of thumb: end the last property in the list with a comma to make it easier to add/remove/move around properties.
- Example:
-
Square brackets
- For multiword properties, the dot access doesn’t work.
- Instead we use the square bracket notation. For example:
let user = { name: "John", age: 30, "likes birds": true }; //Now, to alert alert(user["likes birds"]); // true
-
Property value shorthand
- We can write this:
function makeUser(name, age) { return { name: name, age: age, // ...other properties }; } let user = makeUser("John", 30); alert(user.name); // John
- For shorthand, we can use
name
instead ofname:name
, like this:function makeUser(name, age) { return { name, // same as name: name age, // same as age: age // ... }; }
- We can use both normal properties and shorthands in the same object.
- We can write this:
-
Property names limitations
- A variable cannot have a name equal to one of the language-reserved words. But for an object property, there’s no such restriction. They can be any strings or symbols.
- Other types are automatically converted to strings. For instance, a number
0
becomes a string"0"
when used as a property key.
-
Property existence test, “in” operator
- In JavaScript, it is possible to access any property in an object. There will be no error if the property doesn’t exist!
- Reading a non-existing property just returns
undefined
. - There’s also a special operator
"in"
for that.let user = { name: "John", age: 30 }; alert( "age" in user ); // true, user.age exists alert( "blabla" in user ); // false, user.blabla doesn't exist
-
The "for..in" loop
- Syntax:
for (key in object) { // executes the body for each key among object properties }
- Syntax:
-
Order of an Object
- Integer properties are sorted, others appear in creation order. The details follow.
- For example:
let codes = { "41": "Switzerland", "49": "Germany", "1": "USA", }; for (let code in codes) { alert(code); // 1, 41, 49 }
- If the keys are non-integer, then they are listed in the creation order.
let user = { name: "John", surname: "Smith" }; user.age = 25; // add one more // non-integer properties are listed in the creation order for (let prop in user) { alert( prop ); // name, surname, age }
- To fix the issue with the integer sort, we can “cheat” by making the codes non-integer. Adding a plus
"+"
sign before each key is enough(ex:+49
).