OBJECTS Flashcards

1
Q

1) An object is a composite value?:-

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?:-

A

—> 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.

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

3) In addition to maintaining its own set of properties, a JavaScript object also inherits?:-

A

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.

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

4) JavaScript objects are dynamic?:—

A

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.

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

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?:-

A

immutable objects.

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

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?:-

A

copy of that object. Any modifications made to the object through the variable y are also visible through the variable x.

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

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?:-

A

own property to refer to non-inherited properties.

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

8) In addition to its name and value, each property has three property attributes:

A

-> 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.

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

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?:-

A

writable, enumerable, and configurable.

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

10) There are three ways object can be created in js, list them?:-

A

-> 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.

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

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?:-

A

comma-separated list of colon-separated name:value pairs, enclosed within curly braces.

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

12) A property name is a JavaScript __________. A property value is any JavaScript __________?:-

A

-> 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.

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

13) The new operator creates and initializes a new object. The new keyword must be followed by a?:-

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

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

14) Almost every JavaScript object has a second JavaScript object associated with it. This second object is known as?:-

A

a prototype, and the first object inherits properties from the prototype.

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

15) All objects created by object literals have the same prototype object, and we can refer to this prototype object in JavaScript code as?:-

A

Object.prototype.

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

16) Objects created using the new keyword and a constructor invocation use the value of the prototype property of the constructor function as their?:-

A

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
Q

17) Object.prototype is one of the rare objects that has no prototype(meaning)?:-

A

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
Q

18) What is a prototype chain?:-

A

The linked series of prototype objects is known as a prototype chain

19
Q

19) Object.create() creates a new object, using its first argument as?:-

A

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
Q

20) You can pass __________ to create a new object that does not have __________, but if you do this, the newly
created object will not?:-

A

-> 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
Q

21) Object.create() also takes an optional second argument that?:-

A

describes the properties of the new object.

22
Q

22) One use for Object.create() is when you want to guard against?:-

A

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
Q

23) To obtain the value of a property, use the __________ or __________ operators?:-

A

-> 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
Q

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)?:-

A

book.edition = 7; // Create an “edition” property of book.
book[“main title”] = “ECMAScript”; // Change the “main title” property.

25
Q

25) object.property, or object[“property”]. The first syntax, using the dot and an identifier, is like the syntax
used to access a static field of a struct or object in C or Java. The second syntax, using square brackets and a
string, looks like array access, but to an array indexed by strings rather than by numbers. This kind of array is
known as an?:-

A

associative array (or hash or map or dictionary).

  • JavaScript objects are associative arrays.
26
Q

26) When you use the . operator to access a property of an object, however, the name of the property is expressed as an identifier. Identifiers must be typed literally into your JavaScript program; they are not a datatype, so they cannot be manipulated by the program. On the other hand, when you access a property of an object with the [] array notation, the name of the property is expressed as a string. Strings are JavaScript datatypes, so they can be?:-

A

manipulated and created while a program is running. So, for example, you can write the following code in JavaScript:

let addr = “”;
for(let i = 0; i < 4; i++) {
addr += customer[address${i}] + “\n”;
}

This code reads and concatenates the address0, address1, address2, and address3 properties of the customer object.

  • This brief example demonstrates the flexibility of using array notation to access properties of an object with
    string expressions. This code could be rewritten using the dot notation, but there are cases in which only the array notation will do. Suppose, for example, that you are writing a program that uses network resources to compute the current value of the user’s stock market investments. The program allows the user to type in the name of each stock they own as well as the number of shares of each stock. You might use an object named portfolio to hold this information. The object has one property for each stock. The name of the property is the name of the stock, and the property value is the number of shares of that stock. So, for example, if a user holds 50 shares of stock in IBM, the portfolio.ibm property has the value 50.

Part of this program might be a function for adding a new stock to the portfolio:

function addstock(portfolio, stockname, shares) {
portfolio[stockname] = shares;
}

Since the user enters stock names at runtime, there is no way that you can know the property names ahead of time.
Since you can’t know the property names when you write the program, there is no way you can use the . operator to access the properties of the portfolio object. You can use the [] operator, however, because it uses a string value (which is dynamic and can change at runtime) rather than an identifier (which is static and must be hardcoded in the program) to name the property.