Be insensitive, and keep a lifelong growth mindset.

0%

## 1. Everything is and Object: Inheritance and the Prototype Chain

### 1.1. Object-Oriented Programming

• Objects interacting with one another through methods and properties;
• Used to store data, structure applications into modules and keeping code clean.

### 1.3. Inheritance in JavaScript: Prototypes and Prototype Chains

The inheritance in javaScript depends on “Prototype” property.

### 1.4. Summary

• Every JavaScript object has a prototype property, which makes inheritance possible in JavaScript
• The prototype property of an object is where we put methods and properties that we want other objects to inherit.
• The Constructor’s prototype property is NOT the prototype of the Constructor itself, it’s the prototype of ALL instances that are created through it.
• When a certain method (or property) is called, the search starts in the object itself, and if it cannot be found, the search moves on to the object’s prototype. This continues until the method is found: prototype chain.

## 2. Creating Objects: Function Constructors

The first way is to define all properties and functions in constructor:

The alternative way is to use prototype property:

## 3. The Prototype Chain in the Console

In console, if we type in ‘john’, we can see the john object gets printed.

From the above screenshot, we can see Person’s prototype is an Object, and also

From the second screenshot, we can see that because of the inheritance, the john object can call hasOwnProperty method. Obviously, job is john’s own property, but lastName is not.

Another method to know is instanceof, we can know john is an instance of Person object.

By typing console.info(x), we can see this array object. If you click on the proto, you can see all methods available in array.

## 4. Creating Objects: Object.create

Object.create builds an object that inherits directly from the one that we passed into the firs argument.

While, on the other hand, the function constructor, the newly created object inherits from constructor’s prototype property.

Actually, one of the biggest benefits of object.create is that it allows us to implement a really complex inheritance structures in an easier way, because it allows us to directly specify which object should be a prototype.

## 5. Primitives vs Objects

Variables containing primitives actually hold that data inside of the variable itself.

However, variables associated with objects do not actually contain the object, but instead, they contain a reference to the place in memory where the object is stored.

So again, a variable declared as an object does not have a real copy of the object, it just points to that object.

See the example

In console, we can see the print out as follows

It shows that actually obj1 and obj2 are pointing to the same object, once obj1.age gets changed, it reflects to obj2.age as well.

Another example

When we pass a primitive into a function, a simple copy is created. You can change a as much as you want, but it will never affect the variable on the outside.

For the object, instead of passing the object to the function, we just pass in a reference.

## 6. First Class Functions: Passing Functions as Arguments

### 6.1. Functions are Also Objects in JavaScript

• A function is an instance of the Object type;
• A function behaves like any other object;
• We can store functions in a variable;
• We can pass a function as an argument to another function;
• We can return a function from a function

Because of all above, we say that in JavaScript, we have First-Class Functions.

## 7. First Class Functions: Functions Returning Functions

Functions can also be returned from functions.

## 8. Immediately Invoked Function Expressions (IIFE)

Here we use a silly game to demonstrate how IIFE works,

IIFE can only be called once and has the advantage of data privacy, because unlike defining a function and assigning it to a variable, IIFE can not be reused elsewhere.

## 9. Closures

### 9.2. Closures Summary

An inner function has always access to the variables and parameters of its outer function, even after the outer function has returned.

### 9.3. How Closures Work

After the retirement function returns, the execution context of retirement function is gone from the Execution Stack. However, in the scope chain, the retirement function is still there and keeps working. Therefore, we can access the variables that we were created in the retirement function long after the function has completed execution, and after its execution context is gone.

## 11. Code Challenge： Build a Fun Quiz Game in the Console

### 11.1. Basic Level

1. Build a function constructor called Question to describe a question. A question should include:
a) question itself
b) the answers from which the player can choose the correct one (choose an adequate data structure here, array, object, etc.)
c) correct answer (I would use a number for this)

2. Create a couple of questions using the constructor

3. Store them all inside an array

4. Select one random question and log it on the console, together with the possible answers (each question should have a number) (Hint: write a method for the Question objects for this task).

5. Use the ‘prompt’ function to ask the user for the correct answer. The user should input the number of the correct answer such as you displayed it on Task 4.

6. Check if the answer is correct and print to the console whether the answer is correct ot nor (Hint: write another method for this).

7. Suppose this code would be a plugin for other programmers to use in their code. So make sure that all your code is private and doesn’t interfere with the other programmers code (Hint: we learned a special technique to do exactly that).

### 11.2. Expert Level

1. After you display the result, display the next random question, so that the game never ends (Hint: write a function for this and call it right after displaying the result)

2. Be careful: after Task 8, the game literally never ends. So include the option to quit the game if the user writes ‘exit’ instead of the answer. In this case, DON’T call the function from task 8.

3. Track the user’s score to make the game more fun! So each time an answer is correct, add 1 point to the score (Hint: I’m going to use the power of closures for this, but you don’t have to, just do this with the tools you feel more comfortable at this point).

4. Display the score in the console. Use yet another method for this.