Objects Flashcards
let user = new Object();
// the above is what type of object?
“object constructor” syntax
let user = {};
// the above is what type of object?
“object literal” syntax
an object has a property named?
key
an objects key contains a ____?
value
in a object a key can have multiple words but it must use ?
quoted
multi word keys will not work with what and must use _____?
dot notation
brackets
using bracket notation:
let user = {};
set a key “name” to “steve”
user[“name”]=”steve”;
let user = {
names = “steve”
};
delete names using bracket notation.
delete user[‘names’];
let user = {
names = “steve”
};
delete names using dot notation.
delete user.names
let user = {
name: “John”,
age: 30
};
// print “30” using dot notation
// print “30” using brackets
console.log(user.age)
console.log(user[“age”]);
write an object and add the number 5 to “bag” object
console.log(bag.apple);
let bag = {
[fruit]: 5,
};
function makeUser(name, age) {
return {
name: name,
age: age,
};
}
How can the above be shortened and why?
function makeUser(name, age) {
return {
name,
age,
};
}
properties have the same names as variables
let obj = {
for: 1,
let: 2,
return: 3
};
Will this give an error and why?
no
In short, there are no limitations on property names
let obj = {
0: “test”
};
alert( obj[“0”] ); // returns
test
let obj = {
0: “test”
};
alert( obj[0] ); // returns
test
let user = {};
console.log( user.noSuchProperty === undefined );
// returns and why?
true
here will be no error if the property doesn’t exist!
Reading a non-existing property just returns undefined
let user = { name: “John”, age: 30 };
alert( “age” in user ); //
alert( “blabla” in user ); //
true
false
let obj = {
test: undefined
};
alert( obj.test ); // Returns?
alert( “test” in obj ); // Returns?
and why
undefined
true
In the code above, the property obj.test technically exists. So the in operator works right.
for unknown or empty values we use _____
null
let user = {
name: “John”,
age: 30,
isAdmin: true
};
// the below returns?
for (let key in user) {
alert( key );
}
name, age, isAdmin
let user = {
name: “John”,
age: 30,
isAdmin: true
};
// the below returns?
for (let key in user) {
alert( user[key] );
}
John, 30, true
let codes = {
“49”: “Germany”,
“41”: “Switzerland”,
“44”: “Great Britain”,
“1”: “USA”
};
for (let code in codes) {
alert(code);
}
// returns and why?
1, 41, 44, 49
integer properties are sorted, others appear in creation order.
let user = {
name: “John”,
surname: “Smith”,
age: 25
};
for (let prop in user) {
alert( prop );
}
returns?
// name, surname, age
let user = {
name: “John”,
surname: “Smith”,
age: 25
};
//write a for in loop so it shows name, surname and age
for (let prop in user) {
alert( prop );
}
let user = {
name: “John”,
surname: “Smith”,
age: 25
};
write a for in loop so it shows
john, smith, 25
for (let prop in user) {
alert( user[prop] );
}
let codes = {
“49”: “Germany”,
“41”: “Switzerland”,
“44”: “Great Britain”,
“1”: “USA”
};
for (let code in codes) {
alert(code); /
}
what to do write make this print 1, 41, 44, 49.
add + to all keys