Objects Flashcards
(140 cards)
What is an object ?
An object is a composite value: it aggregates multiple values (primitive values or other objects) and allows you to store and retrieve those values by name.
An object is an unordered collection of properties, each of which has a name and a value. Property names are usually strings (although, as we’ll see in §6.10.3, property names can also be Symbols), so we can say that objects map strings to values.
What are the other common names of “string-to-value” mapping structures in other programming languages ?
hash, hashtable, dictionary, associative array
What makes a javascript object something more than a simple “string-to-value” map ?
A javascript object also inherits the properties of another object, known as its “prototype”.
What are types possible for the property names of an object
String (most common one) and Symbol.
The other types are converted to String.
What are the possible values of a javascript object property ?
Any javascript value, or a getter or setter function (or both)
What is a prototype ?
A prototype is an object that make one or many other objects inherits of its properties.
Almost every javascript object has a second javascript object associated with it: its prototype.
What property of the constructor function is used as the prototype for objects created with the new keyword?
The value of the prototype property of the constructor function
This means that the newly created object inherits properties and methods from the constructor’s prototype.
True or False: Objects created using the new keyword do not inherit from the constructor’s prototype.
False
Objects created with the new keyword inherit from the prototype of the constructor function.
What happens to the prototype of an object created using a constructor function?
It uses the value of the prototype property of the constructor function
This establishes a prototype chain for the newly created object.
What is unique about Object.prototype?
It has no prototype and does not inherit any properties.
This makes Object.prototype a rare object in JavaScript.
Do most built-in constructors inherit from Object.prototype?
Yes, most built-in constructors and user-defined constructors have a prototype that inherits from Object.prototype.
Examples include constructors like Date, Array, and others.
What is the term for the linked series of prototype objects?
Prototype chain.
This chain allows objects to inherit properties from one another in JavaScript.
True or False: Object.prototype inherits properties from another object.
False.
Object.prototype is the base object and does not have a prototype.
Fill in the blank: Most user-defined constructors have a prototype that inherits from __________.
Object.prototype.
This is a fundamental aspect of JavaScript’s prototypal inheritance.
What does Object.create() do?
Creates a new object, using its first argument as the prototype of that object
What properties does o1 inherit in the example let o1 = Object.create({x: 1, y: 2})?
x and y
What happens when you pass null to Object.create()?
Creates a new object that does not have a prototype, inheriting no properties or methods
What is the effect of creating an object with let o2 = Object.create(null)?
o2 inherits no properties or methods
How do you create an ordinary empty object using Object.create()?
Pass Object.prototype as the argument
What is the example code for creating an ordinary empty object with Object.create()?
let o3 = Object.create(Object.prototype)
What does the second argument of Object.create() do?
Describes the properties of the new object (advanced feature)
What is one use case for Object.create() mentioned in the text?
Guard against unintended modifications of an object by a library function
What is the purpose of passing Object.create(o) to a library function?
To prevent accidental modifications to the original object
What will happen if the library function sets properties on the object created by Object.create(o)?
Those writes will not affect the original object