Chapter 6: Objects Flashcards

(38 cards)

1
Q

Any value in JavaScript that is not a string, a number, a Symbol, or
true, false, null, or undefined is an _____

A

object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to create an object

A

Object.create()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In its simplest form, an ____ ____ is a commaseparated list of colon-separated name:value pairs, enclosed within
curly braces

A

object literal
let empty = {}; // An object with no
properties
let point = { x: 0, y: 0 }; // Two numeric
properties
let p2 = { x: point.x, y: point.y+1 }; // More complex

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

let o = new Object();

A

// Create an empty object: same as {}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

let a = new Array();

A

// Create an empty array: same as [].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

let r = new Map();

A

// Create a Map object for key/value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Example of object creation

A

let o1 = Object.create({x: 1, y: 2});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

You can pass null to create a new object that does not have a
prototype, but if you do this, the newly created object will not _____
anything, not even basic methods like toString()

A

inherit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Creating any empty object

A

let o3 = Object.create(Object.prototype); // o3 is like {} or
new Object().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

A collection of related properties and/or methods

A

objects

object = {key:value, function()}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

To obtain the value of a property, use the _____ or ______ ______

A

dot (.), square bracket ([])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If
using square brackets, the value within the brackets must be an
expression that evaluates to a ______ that contains the desired property

A

string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Are these two expressions the same?
object.property
object[“property”]

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Example

A

let unitcircle = { r: 1 }; // An object to inherit
from
let c = Object.create(unitcircle); // c inherits the property r
c.x = 1; c.y = 1; // c defines two properties of its own
c.r = 2; // c overrides its
inherited property
unitcircle.r // => 1: the prototype is
not affected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Property access expressions will fail if the lefthand side of the. is
____ or _______.

A

null, undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Attempting to set a property on ____ or ______ also causes a
TypeError

A

null, undefined

17
Q

The delete operator (§4.13.4) removes a ______ from an object

18
Q

delete does not operate on the value of the property but on the
property itself

A

True
delete book.author; // The book object now has no
author property.
delete book[“main title”]; // Now it doesn’t have “main
title”, either.

19
Q

The ___________ method of an object tests whether that
object has an own property with the given name. It returns false for
inherited properties:

A

hasOwnProperty()
let o = { x: 1 };
o.hasOwnProperty(“x”) // => true: o has an own
property x
o.hasOwnProperty(“y”) // => false: o doesn’t have a
property y
o.hasOwnProperty(“toString”) // => false: toString is an
inherited property

20
Q

____ can distinguish between
properties that do not exist and properties that exist but have been set to
undefined

A

in

Ex:
let o = { x: undefined }; // Property is explicitly set to
undefined
o.x !== undefined // => false: property exists but
is undefined
o.y !== undefined // => false: property doesn’t even
exist
“x” in o // => true: the property exists
“y” in o // => false: the property doesn’t
exist
delete o.x; // Delete the property x
“x” in o // => false: it doesn’t exist
anymore

21
Q

There are four functions you can use to get an
array of property names

A

Object.keys()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Reflect.ownKeys() –> returns all own property names both enumerable , and obth string and Symbol

22
Q

________ copies properties with ordinary property get and
set operations, so if a source object has a getter method or the target
object has a setter method, they will be invoked during the copy, but
they will not themselves be copied.

A

Object.assign

23
Q

Example of Object.assign()

A

Object.assign(o, defaults); // overwrites everything in o
with defaults

24
Q

As discussed earlier, all JavaScript objects (except those explicitly
created without a prototype) inherit properties from
__________

A

Object.prototype()

25
Every object created using {} or new Object() automatically links to ________
Object.prtotype
26
The ______ method takes no arguments; it returns a string that somehow represents the value of the object on which it is invoked
toString()
27
. The purpose of this method is to return a localized string representation of the object
toLocaleString()
28
The _______ method is much like the toString() method, but it is called when JavaScript needs to convert an object to some primitive type other than a string—typically, a number
valueOf()
29
Another example of object
let square = { area: function() { return this.side * this.side; }, side: 10 }; square.area() // => 100
30
Ways to access object properties
1. object.property .2. Bracket Notation (object["property"])
31
Accessor properties are represented by “getter” and “setter” methods. In an object literal they are denoted by ____ and ____
get, set let user = { name: "John", surname: "Smith", get fullName() { return `${this.name} ${this.surname}`; } }; alert(user.fullName); // John Smith
32
let a = {}; let b = {}; // two independent objects alert( a == b );
// false
33
Object.assign(dest, ...sources)
The first argument dest is a target object. Further arguments is a list of source objects. It copies the properties of all source objects into the target dest, and then returns it as the result.
34
The call structuredClone(object) clones the object with all nested properties.
let user = { name: "John", sizes: { height: 182, width: 50 } }; let clone = structuredClone(user); alert( user.sizes === clone.sizes ); // false, different objects // user and clone are totally unrelated now user.sizes.width = 60; // change a property from one place alert(clone.sizes.width); // 50, not related
35
To access the object, a method can use the ___ keyword
this
36
In JavaScript, keyword this behaves unlike most other programming languages. It can be used in any function, even if it’s not a ______ of an _____
method, object
37
Arrow functions have no this
true
38
When a function is executed with new, it does the following steps:
A new empty object is created and assigned to this. The function body executes. Usually it modifies this, adds new properties to it. The value of this is returned.