Objects Flashcards
var person = {
name : “steve”,
age : 24,
}
two ways to access
person[‘name’]
person.name
var person = {
name : “steve”,
age : 24,
}
set name to “bob”
person.name = "bob" person["name"] = "bob"
var person = {
name : “steve”,
age : 24,
}
add country Brazil
person.country = "Brazil" person["country'] = "Brazil"
let user = { name: “John”, age: 30 };
alert( “age” ________); // true, user.age exists
in user
let user = { name: "John", age: 30, isAdmin: true };
for (let key in user) {
alert( key ); //
}
name, age, isAdmin
let user = { name: "John", age: 30, isAdmin: true };
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); //
}
1, 41, 44, 49
let codes = { "+49": "Germany", "+41": "Switzerland", "+44": "Great Britain", // .., "+1": "USA" };
for (let code in codes) {
alert( code ); //
}
49, 41, 44, 1
in objects ____________ properties are sorted, others appear in creation order.
integer
let a = {}; let b = {};
alert( a == b ); //
false
An object declared as const can or cannot be changed.
can
let user = { name: “John”, age: 30 };
alert( “blabla” in user ); //
false, user.blabla doesn’t exist
let a = {}; let b = a;
alert( a == b );
true
let a = {}; let b = a;
alert( a === b ); //
true
let a = {}; let b = {}; alert( a == b ); //
false
We also can use ____________ to replace the loop for simple cloning:
Object.assign
let user = { name: "John", age: 30 };
Clone using Object.assign
let clone = object.assign({}, user);
let user = { name: "John", sizes: { height: 182, width: 50 } };
alert( user.sizes.height ); //
182
let user = { name: "John", sizes: { height: 182, width: 50 } };
alert( _________ ); //50
user.sizes.width
The object constructor, which uses the______ keyword
new
ADD NEW METHOD NAMED “COLOR”
let user = { name: "John", age: 30 };
user.color = function() { //code }
// Initialize an object const employees = { boss: 'Michael', secretary: 'Pam', sales: 'Jim', accountant: 'Oscar' };
// Get the keys of the object const keys = \_\_\_\_\_\_\_\_(employees);
console.log(keys);
object.keys
let user = { name: "John", age: 30, isAdmin: true };
for (let key in user) {
alert(________);
} name, age, isAdmin
key
let user = { name: "John", age: 30, isAdmin: true };
for (let key in user) {
alert(________);
} john, 30, true
user[key]
const job = { position: 'cashier', type: 'hourly', isAvailable: true, };
// const barista = //create a new JOB object
Object.create(job);
The ___________ method creates a new object, using an existing object as the prototype of the newly created object.
Object.create()
The ___________ method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
Object.assign()
a _______ is something you can do with an object
method
The _______ determines if an object is frozen.
Object.isFrozen()
const object1 = { property1: 42 };
console.log(Object.isFrozen(object1)); // expected output:
false
const object1 = { property1: 42 };
console.log(Object.isFrozen(object1)); // expected output: false
Object.freeze(object1);
console.log(Object.isFrozen(object1)); // expected output:
true
const obj = { prop: 42 };
Object.freeze(obj);
obj.prop = 33; //
Throws an error in strict mode