Chapter 6: Objects Flashcards
(30 cards)
Any value in JavaScript that is not a string, a number, a Symbol, or
true, false, null, or undefined is an _____
object
How to create an object
Object.create()
In its simplest form, an ____ ____ is a commaseparated list of colon-separated name:value pairs, enclosed within
curly braces
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
let o = new Object();
// Create an empty object: same as {}.
let a = new Array();
// Create an empty array: same as [].
let r = new Map();
// Create a Map object for key/value
Example of object creation
let o1 = Object.create({x: 1, y: 2});
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()
inherit
Creating any empty object
let o3 = Object.create(Object.prototype); // o3 is like {} or
new Object().
A collection of related properties and/or methods
objects
object = {key:value, function()}
To obtain the value of a property, use the _____ or ______ ______
dot (.), square bracket ([])
If
using square brackets, the value within the brackets must be an
expression that evaluates to a ______ that contains the desired property
string
Are these two expressions the same?
object.property
object[“property”]
yes
Example
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
Property access expressions will fail if the lefthand side of the. is
____ or _______.
null, undefined
Attempting to set a property on ____ or ______ also causes a
TypeError
null, undefined
The delete operator (§4.13.4) removes a ______ from an object
property
delete does not operate on the value of the property but on the
property itself
True
delete book.author; // The book object now has no
author property.
delete book[“main title”]; // Now it doesn’t have “main
title”, either.
The ___________ method of an object tests whether that
object has an own property with the given name. It returns false for
inherited properties:
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
____ can distinguish between
properties that do not exist and properties that exist but have been set to
undefined
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
There are four functions you can use to get an
array of property names
Object.keys()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Reflect.ownKeys() –> returns all own property names both enumerable , and obth string and Symbol
________ 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.
Object.assign
Example of Object.assign()
Object.assign(o, defaults); // overwrites everything in o
with defaults
As discussed earlier, all JavaScript objects (except those explicitly
created without a prototype) inherit properties from
__________
Object.prototype()