Objects JS Flashcards
In objects, what is a property?
A “key:value” pair, where key is a string (the name of the property) and the value van be anything
let user = new Object(); // “object constructor” syntax
let user = {}; // “object literal” syntax
What is happening here ?
Empty object is being created
Let’s create an object for an user, where we know the name and the age of the user.
let user = { // an object
name: “John”, // by key “name” store value “John”
age: 30 // by key “age” store value 30
};
How to access property values?
Property values are accessible using the dot notation:
alert( user.name ); // John
How to remove a property?
To remove a property, we can use the delete operator:
delete user.age;
What is a multiword property name?
We can also use multiword property names, but then they must be quoted:
“likes birds”: true
For multiword properties, the dot access doesn’t work. What do we use instead?
There’s an alternative “square bracket notation” that works with any string:
user[“likes birds”] = true;
What is a computed property?
We can use square brackets in an object literal, when creating an object. That’s called computed properties.
let fruit = prompt(“Which fruit to buy?”, “apple”);
let bag = {
[fruit]: 5, // the name of the property is taken from the variable fruit
};
alert( bag.apple ); // 5 if fruit=”apple”
The meaning of a computed property is simple: [fruit] means that the property name should be taken from fruit.
So, if a visitor enters “apple”, bag will become {apple: 5}.
The use-case of making a property from a variable is so common, that there’s a special property value shorthand to make it shorter. How would that be ?
Instead of name:name we can just write name, like this:
function makeUser(name, age) {
return {
name, // same as name: name
age, // same as age: age
// …
};
}
Can we use both normal properties and shorthands in the same object?
Yes!
let user = {
name, // same as name:name
age: 30
};
As we already know, a variable cannot have a name equal to one of the language-reserved words like “for”, “let”, “return” etc.
Does the same apply for object properties?
No! for an object property, there’s no such restriction, there are no limitations on property names.
Other types are automatically converted to strings.
For instance, a number 0 becomes a string “0” when used as a property key.
What is the Property existence test, “in” operator?
So we can easily test whether the property exists:
There’s also a special operator “in” for that.
The syntax is:
“key” in object
let user = { name: “John”, age: 30 };
alert( “age” in user ); // true, user.age exists
To walk over all keys of an object, we use?
The “for..in” loop
The syntax:
for (key in object) {
// executes the body for each key among object properties
}
If we loop over an object, do we get all properties in the same order they were added?
“ordered in a special fashion”: integer properties are sorted, others appear in creation order.
Integer properties? What’s that?
The “integer property” term here means a string that can be converted to-and-from an integer without a change.