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