Objects Flashcards

1
Q

JS objects are simple collections of named … with ….

A

JavaScript objects are simple collections of named properties with values

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

JavaScript inheritance uses ….

A

Prototypes and a prototype chain, known as prototypal inheritance

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

Every object can have a reference to a … but it differs from the … on a …

A

Prototype, prototype property, function

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

We can define the prototype of an object by using the … method

A

Object.setPrototypeOf()

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

Every function has a …. property

A

Prototype property, not to be confused with the __proto__ / [[Prototype]] link on an object.

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

A function’s prototype property has a … property pointing back to itself

A

constructor

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

What is returned from the Person function?

function Person() {}

const x = new Person();

A

An object - an instance of the Person ‘class’.

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

The ES6 class syntax is really what under the hood?

A

Syntactic sugar for the constructor function / new keyword technique for prototypal inheritance

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

What does the super() call do?

A

It calls the parent class’s constructor function

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

Which of the following properties points to an object that will be searched if the target object doesn’t have the searched-for property?

  • a class
  • b instance
  • c prototype
  • d pointTo
A

c) prototype

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

After running the following code, what will ninja.constructor point to?

function Person() {}

function Ninja() {}

Ninja.prototype = new Person();

const ninja = new Ninja();

A

ninja.constructor will point to Person()

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

The Object.setPrototypeOf() method sets the … of a specified object to … or …

A

The Object.setPrototypeOf() method sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null.

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

What are the parameters a & b in the following example?

Object.setPrototypeOf(a, b)

A

a: target object - the object which is to have its prototype set
b: prototype object - the object’s new prototype (an object or null)

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

What is the return value of Object.setPrototypeOf()?

A

The specified object that has its prototype changed

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

What is printed to the console?

const a = { speed: 100 };

const x = Object.setPrototypeOf({}, a);

console.log(x.speed);

A

100

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

How many parent object’s can the [[Prototype]] property hold?

A

1

17
Q

The Object.create method creates a new …

A

Object

18
Q

How many parameters does the Object.create() method take?

A

At least 1, the prototype object. There is also an optional properties object:

Object.create(proto[, propertiesObject])

19
Q

What are the paramters a & b?

Object.create(a,b);

A

a: prototype object - the object that will be used as the prototype
b: properties object - an optional object

20
Q

What is the return value of Object.create?

A

A new object with the specifed prototype object and properties

21
Q

Can inheritance using Object.create be detected by instanceof? Why?

A

No. instanceof works by checking prototype constructors, and Object.create does not use constructors.

22
Q

What can we use instead of instanceof to check prototypal inheritance using Object.create?

A

parentObject.isPrototypeOf(child) // boolean

23
Q

Do objects have prototypes?

A

Yes

24
Q

What is the difference between a function’s prototype and an object’s prototype?

A

All objects have a prototype, refered to by the __proto__ / chain property *. Functions are objects. Functions have prototypes.

However - this is confusing because there is a __proto__ (dunder proto) and an explicit .prototype property. Functions have both of these.

Foo.prototype is an explicit property of Foo:

Foo.prototype === Foo.__proto__ // false

[[Prototype]] is the actual object that is used in the prototypal lookup chain. Try to think of the underscores as being a chain __proto__ (the __proto__ is not supported by all engines).

* Objects can be created with their prototypes deliberately set to null, in which case searching their prototype returns undefined.

25
Q

Does JavaScript have private object properties?

A

No.

26
Q

Name 3 ways to mimic privacy within JavaScript

A
  1. ) Via closure
  2. ) Via Object.defineProperty creating setters & getters
  3. ) Via Symbols