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
The __________ method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed.
Object.freeze()
The __________method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.
Object.seal()
const object1 = { property1: 42 };
Object.seal(object1);
object1.property1 = 33;
console.log(object1.property1);
// expected output: ————->
delete object1.property1; // ------> console.log(object1.property1); // expected output: --------->
33
33
The__________ method determines if an object is sealed.
Object.isSealed()
const object1 = { property1: 42 };
console.log(Object.isSealed(object1)); // expected output:
Object.seal(object1);
console.log(Object.isSealed(object1)); // expected output:
false
true
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()
const target = { a: 1, b: 2 }; const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target); // expected output:
console.log(returnedTarget); // expected output:
Object { a: 1, b: 4, c: 5 }
Object { a: 1, b: 4, c: 5 }
The_________ method returns an array of a given object’s own property names, in the same order as we get with a normal loop.
Object.keys()
const object1 = { a: 'somestring', b: 42, c: false };
console.log(Object.keys(object1)); // expected output:
Array [“a”, “b”, “c”]
const object1 = { a: 'somestring', b: 42, c: false };
console.log(\_\_\_\_\_\_\_\_\_\_(object1)); // expected output: Array ["a", "b", "c"]
const object1 = { a: 'somestring', b: 42, c: false };
console.log(Object.keys(object1)); // expected output: Array ["a", "b", "c"]
var arr = ['a', 'b', 'c']; console.log(Object.keys(arr)); // console:
[‘0’, ‘1’, ‘2’]
var obj = { 0: 'a', 1: 'b', 2: 'c' }; console.log(Object.keys(obj)); // console:
[‘0’, ‘1’, ‘2’]
var anObj = { 100: 'a', 2: 'b', 7: 'c' }; console.log(Object.keys(anObj)); // console:
[‘2’, ‘7’, ‘100’]
var anObj = { "+100": 'a', "+2": 'b', "+7": 'c' }; console.log(Object.keys(anObj)); // console:
[‘100’, ‘2’, ‘7’]
The________ method returns an array of a given object’s own enumerable property values, in the same order as that provided by a for…in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
Object.values()
const object1 = { a: 'somestring', b: 42, c: false };
console.log(Object.values(object1)); // expected output:
Array [“somestring”, 42, false]
const object1 = { a: 'somestring', b: 42, c: false };
console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_); // expected output: Array ["somestring", 42, false]
(Object.values(object1)
The _____ determines if an object is frozen.
The Object.isFrozen() determines if an object is frozen.
const object1 = { property1: 42 };
console.log(Object.isFrozen(object1)); // expected output: ------------>
Object.freeze(object1);
console.log(Object.isFrozen(object1)); // expected output: ---------------->
false
true
The _______method returns a string representing the source code of the function.
toString()
function sum(a, b) { return a + b; } console.log(sum.toString()); // expected output: ------->
“function sum(a, b) {
return a + b};
function sum(a, b) { return a + b; }
console.log(\_\_\_\_\_\_\_\_\_\_\_); // expected output: "function sum(a, b) { // return a + b; // }"
sum.toString());
The _________ method determines if an object is extensible (whether it can have new properties added to it).
Object.isExtensible()
const object1 = {};
console.log(Object.isExtensible(object1)); // expected output:
true
// New objects are extensible. var empty = {};
TRUE/FALSE
TRUE
var sealed = Object.seal({}); Object.isExtensible(sealed); // ===
TRUE/FALSE
false
TRUE/FALSE
var frozen = Object.freeze({}); Object.isExtensible(frozen); // ===
false
function Dog(name) { this.name = name; }
var dog1 = new Dog(‘Gabby’);
Dog.prototype.toString = function dogToString() {
return ‘’ + this.name;
}
console.log(dog1.toString()); // expected output:
“Gabby”
The __________method checks if an object exists in another object’s prototype chain.
isPrototypeOf()
function object1() {} function object2() {}
const object3 = new object1(); console.log(object2.prototype.isPrototypeOf(object3));
false
function object1() {} function object2() {}
object1.prototype = Object.create(object2.prototype);
const object3 = new object1();
console.log(object1.prototype.isPrototypeOf(object3)); // expected output: --------->
console.log(object2.prototype.isPrototypeOf(object3)); // expected output: ---------->
true
true
The ____________ method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
hasOwnProperty()
const object1 = new Object(); object1.property1 = 42;
console.log(object1.hasOwnProperty('property1')); // expected output:
true
const object1 = new Object(); object1.property1 = 42;
console.log(object1.hasOwnProperty('toString')); // expected output:
false
const object1 = new Object(); object1.property1 = 42;
console.log(object1.hasOwnProperty('hasOwnProperty')); // expected output:
false
hasOwnProperty returns true even if the value of the property is_____ or________.
null
undefined
o = new Object();
o. propOne = null;
o. hasOwnProperty(‘propOne’); // returns
o. propTwo = undefined;
o. hasOwnProperty(‘propTwo’); // returns
true
true
o = new Object();
o. hasOwnProperty(‘prop’); // returns
o. prop = ‘exists’;
o. hasOwnProperty(‘prop’); // returns
false
true
o = new Object();
o. prop = ‘exists’;
o. hasOwnProperty(‘prop’); // returns
true
o = new Object();
o. prop = ‘exists’;
o. hasOwnProperty(‘toString’); // returns
false
const prototype1 = {}; const object1 = Object.create(prototype1);
console.log(Object.getPrototypeOf(object1) === prototype1); // expected output:
true
var proto = {}; var obj = Object.create(proto); Object.getPrototypeOf(obj) === proto; //
true
function sayHello2(name) { var text = 'Hello ' + name; // Local variable var say = function() { console.log(text); } return say; } var say2 = sayHello2('Bob'); say2(); // logs
“Hello Bob”
function say667() { // Local variable that ends up within closure var num = 42; var say = function() { console.log(num); } num++; return say; } var sayNumber = say667(); sayNumber(); //
logs 43
Without using new keyword how we could use these functions. So JavaScript has 3 different ways to do that:
- call the function as a regular function
- attach them to an object, as its properties
- Use this, which is using call or apply to invoke the function
The ______method calls a function with a given this value and arguments provided as an array (or an array-like object).
apply()
The_________ method calls a function with a given this value and arguments provided individually.
call()
const object1 = new Object(); object1.property1 = 42;
console.log(Object.prototype.hasOwnProperty(‘hasOwnProperty’));
TRUE