OBJECTS Flashcards
(26 cards)
1) 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.
2) Property names are usually strings (although, as we’ll see in later, property names can also be Symbols), so we can say that objects map strings to values. This string-to-value mapping goes by various names?:-
—> you are probably already familiar with the fundamental data structure under the name “hash,” “hashtable,” “dictionary,” or “associative array.” However, an object is more than a simple string-to-value map.
3) In addition to maintaining its own set of properties, a JavaScript object also inherits?:-
the properties of another object, known as its “prototype.” The methods of an object are typically inherited
properties, and this “prototypal inheritance” is a key feature of JavaScript.
4) JavaScript objects are dynamic?:—
properties can usually be added and deleted—but they can be used to simulate the static objects and “structs” of statically typed languages. They can also be used (by ignoring the value part of the string-to-value mapping) to represent sets of strings.
5) Any value in JavaScript that is not a string, a number, a Symbol, or true, false, null, or undefined is an object.
And even though strings, numbers, and booleans are not objects, they can behave like?:-
immutable objects.
6) Recall that objects are mutable and manipulated by reference rather than by value. If the variable x refers to an object and the code let y = x; is executed, the variable y holds a reference to the same object, not a?:-
copy of that object. Any modifications made to the object through the variable y are also visible through the variable x.
7) It is sometimes important to be able to distinguish between properties defined directly on an object and those that are inherited from a prototype object. JavaScript uses the term?:-
own property to refer to non-inherited properties.
8) In addition to its name and value, each property has three property attributes:
-> The writable attribute specifies whether the value of the property can be set.
-> The enumerable attribute specifies whether the property name is returned by a for/in loop.
-> The configurable attribute specifies whether the property can be deleted and whether its attributes can be
altered.
9) Many of JavaScript’s built-in objects have properties that are read-only, non-enumerable, or non-configurable. By default, however, all properties of the objects you create are?:-
writable, enumerable, and configurable.
10) There are three ways object can be created in js, list them?:-
-> objects can be created with object literals,
-> with the new(The new operator creates and initializes a new object) keyword, and
-> with the Object.create() function.
11) The easiest way to create an object is to include an object literal in your JavaScript code. In its simplest form, an object literal is a?:-
comma-separated list of colon-separated name:value pairs, enclosed within curly braces.
12) A property name is a JavaScript __________. A property value is any JavaScript __________?:-
-> identifier or a string literal (the empty string is allowed)
-> expression - the value of the expression (it may be a primitive value or an object value) becomes the value of the property.
13) The new operator creates and initializes a new object. The new keyword must be followed by a?:-
function invocation. A function used in this way is called a constructor and serves to initialize a newly created
object.
JavaScript includes constructors for its built-in types. For example:
let o = new Object(); // Create an empty object: same as {}.
let a = new Array(); // Create an empty array: same as [].
let d = new Date(); // Create a Date object representing the current time
let r = new Map(); // Create a Map object for key/value mapping
14) Almost every JavaScript object has a second JavaScript object associated with it. This second object is known as?:-
a prototype, and the first object inherits properties from the prototype.
15) All objects created by object literals have the same prototype object, and we can refer to this prototype object in JavaScript code as?:-
Object.prototype.
16) Objects created using the new keyword and a constructor invocation use the value of the prototype property of the constructor function as their?:-
prototype.
- So the object created by new Object() inherits from Object.prototype, just as the object created by {} does.
- Similarly, the object created by new Array() uses Array.prototype as its prototype, and the object created by new Date() uses Date.prototype as its prototype. This can be confusing when first learning JavaScript.
Remember: almost all objects have a prototype, but only a relatively small number of objects have a prototype property. It is these objects with prototype properties that define the prototypes for all the other objects.
17) Object.prototype is one of the rare objects that has no prototype(meaning)?:-
it does not inherit any properties. Other prototype objects are normal objects that do have a prototype.
- Most built-in constructors (and most user-defined constructors) have a prototype that inherits from Object.prototype. For example, Date.prototype inherits properties from Object.prototype, so a Date object created by new Date() inherits properties from both?:-
Date.prototype and Object.prototype.
- This linked series of prototype objects is known as a prototype chain.
18) What is a prototype chain?:-
The linked series of prototype objects is known as a prototype chain
19) Object.create() creates a new object, using its first argument as?:-
the prototype of that object:
let o1 = Object.create({x: 1, y: 2}); // o1 inherits properties x and y.
o1.x + o1.y // => 3
20) You can pass __________ to create a new object that does not have __________, but if you do this, the newly
created object will not?:-
-> null
-> a prototype
inherit anything, not even basic methods like toString() (which means it won’t work with the + operator either):
let o2 = Object.create(null); // o2 inherits no props or methods.
- If you want to create an ordinary empty object (like the object returned by {} or new Object()), pass
Object.prototype:
let o3 = Object.create(Object.prototype); // o3 is like {} or new Object().
21) Object.create() also takes an optional second argument that?:-
describes the properties of the new object.
22) One use for Object.create() is when you want to guard against?:-
unintended (but nonmalicious) modification of an object by a library function that you don’t have control over.
- Instead of passing the object directly to the function, you can pass an object that inherits from it. If the
function reads properties of that object, it will see the inherited values. If it sets properties, however, those
writes will not affect the original object.
let o = { x: “don’t change this value” };
library.function(Object.create(o)); // Guard against accidental modifications
- To understand why this works, you need to know how properties are queried and set in JavaScript. These are the topics of the next section.
23) To obtain the value of a property, use the __________ or __________ operators?:-
-> dot (.)
-> square bracket ([])
- The lefthand side should be an expression whose value is an object.
- If using the dot operator, the righthand side must be a simple identifier that names the property.
- If using square brackets, the value within the brackets must be an expression that evaluates to a string(A more precise statement is that the expression must evaluate to a string or a value that can be converted to a string or to a Symbol) that contains the desired property name:
let author = book.author; // Get the “author” property of the book.
let name = author.surname; // Get the “surname” property of the author.
let title = book[“main title”]; // Get the “main title” property of the book.
24) To create or set a property, use a dot or square brackets as you would to query the property, but put them on the lefthand side of an assignment expression(code example)?:-
book.edition = 7; // Create an “edition” property of book.
book[“main title”] = “ECMAScript”; // Change the “main title” property.