Zerotomastery JS advanced Flashcards

1
Q

Which engine google chrome uses ?

A

V8 developed by google. Also used by node js

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

Which language V8 is written ?

A

C++

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

is JS interpreted language ?

A

Depends on the engine. Different engines handle it differently. But initially yes when JS first came out we had SpiderMonkey (still have) which Firefox uses created by Brendon Iche that interpreted javascript to bytecode. But things have envolved now.

We don’t have just interpreters. We also use compilers to optimize our code.

So this is a common misconsooption

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

methods, and things which are bad for optimization

A
  1. eval()
  2. arguments
  3. for in
  4. with
  5. delete

bad for inline caching and hidden classes

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

what is inline caching and give me then example

A

so if function is called multiple times with same input then compiler do optimization and do caching so it will not look up the input every single time. In order to write a optimizable code that engine can do, we have to write a predictible code. Don’t do surprises. Don’t confuse the compiler

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

What is heap memory

A

Heap memory is a place where JavaScript stores objects and functions.
When you create a variable and assign it a primitive value, it will be stored in stack. Something different happens when you try the same, but with an object. When it comes to the object itself, JavaScript will store it in the memory heap. That variable that exists in the stack will only point to this object in memory heap.

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

What is call stack

A

A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc. First in last out

  • When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function.
  • Any functions that are called by that function are added to the call stack further up, and run where their calls are reached.
  • When the current function is finished, the interpreter takes it off the stack and resumes execution where it left off in the last code listing.
  • If the stack takes up more space than it had assigned to it, it results in a “stack overflow” error.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is garbage collection in js

A

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC). The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

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

what is a memory leak

A

A Memory leak can be defined as a piece of memory that is no longer being used or required by an application but for some reason is not returned back to the OS.In simple terms it is forgotten data forever waiting to be used.Leaks are the cause of whole class of problems: slowdowns, crashes, high latency, and even problems with other applications.

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

What is js runtime and web APIS

A

Js runtime is where your code is being exacuting in. Web API is your browsers methods that we are using. They are all async.

E: settimout, fetch.

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

Common ways to cause memory leak

A

Global variables

EventListeners and not removing them

Setinterval

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

What is exacution context ?

A

The Execution Context contains the code that’s currently running, and everything that aids in its execution. During the Execution Context run-time, the specific code gets parsed by a parser, the variables and functions are stored in memory, executable byte-code gets generated, and the code gets executed.

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

What is lexical environment?

A

Lexical Environment is the environment of the function where it is written. That is, the static order/place where it is situated, regardless from where it is called from.

Scope of a variable/function is basically the locations from where a variable is visible/accessible.

Execution context is the status of the execution stack at any point during runtime. That is the current execution context

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

What is scope

A

Scope is the area of the program where an item (be it variable, constant, function, etc.) that has an identifier name is recognized. In our discussion, we will use a variable and the place within a program where the variable is defined determines its scope.

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

What is a scope chain

A

Scope chains establish the scope for a given function. Each function defined has its own nested scope, and any function defined within another function has a local scope which is linked to the outer function — this link is called the chain.

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

Why are global variables considered bad practice?

A

44

They clutter up the global namespace and are slower to look up than local variables.

First of all, having many global variables is always a bad thing because it’s easy to forget you declared a variable somewhere and accidentally re-declare it somewhere else. If your first variable was local then you don’t have a problem. If it was global, then it just got overwritten. This gets even worse when you get into implied globals (e.g. when you say someVar = someValue without declaring someVar with the var keyword).

Secondly, global variables take longer for Javascript to “find” than local variables. The difference in speed isn’t huge, but it does exist.

17
Q

what is THIS keyword

A

This is a object which funciton is property of

18
Q

Context vs Scope

A

Scope pertains to the visibility of the variables, and context refers to the object within which a function is executed

19
Q

What is a closure ?

A

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function

20
Q

What is encapsulation?

A

Encapsulation means that each object in your code should control its own state. State is the current “snapshot” of your object. The keys, the methods on your object, Boolean properties and so on. If you were to reset a Boolean or delete a key from the object, they’re all changes to your state.

Encapsulation is the bundling of data and the methods that act on that data such that access to that data is restricted from outside the bundle. In OOP, that means that an object stores its state privately, and only the object’s methods have access to change it.

21
Q

Prototypal Inheritance

A

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.

22
Q

proto vs prototype

A

__proto__ is an object in every class instance that points to the prototype it was created from.

23
Q

Which one is dynamically scoped and lexically scoped

arrow function or regular function

A

Regular function is dynamically scoped. It changes this keyword based on where its been called

24
Q

Does JS have classes?

A

As syntax sugar yes. Although JavaScript is object-oriented language, it isn’t a class-based language—it’s a prototype-based language. There are differences between these two approaches, but since it is possible to use JavaScript like a class-based language, many people (including myself) often simply refer to the constructor functions as “classes”.

JavaScript classes introduced in ECMAScript 2015 are primarily syntactical sugar over JavaScript’s existing prototype-based inheritance.

25
Q

JS what is the difference of methods defined inside and outside of the constructor function

A

Function defined in the class (not in the constructor) live on the class prototype which each instance is linked to. Functions defined in the constructor become own properties of each instance. If you defined the function in the constructor, each instance will get its own copy of the function. If you don’t the instances will defer to the prototype chain and all the instances will point to the same function. For example:

26
Q

What is implicit and explicit binding in JS?

A

Implicit Binding: Object on the LHS of dot notation. Explicit Binding: Object passed as first parameter in call(), apply(), bind(). New Binding: The LHS object in the function call will be referred to as this in the function. Arrow functions simply use the this value from its surroundings

27
Q

implicit vs explicit binding in JS.

A

In the case of implicit binding, this binds to the object adjacent to the dot(.) operator while invoking the method. Default behaivor where this was refering.

In the case of explicit binding, we can call a function with an object when the function is outside of the execution context of the object. call applly bind

When a function is invoked with the new keyword, the this keyword inside the function binds to the new object being constructed.

When the this keyword is not resolved with any of the bindings, implicit, explicit or new, then this is bound to the window(global) object. In JavaScript’s strict mode, this will be undefined.

In HTML event handlers, this binds to the HTML elements that receive the event.

28
Q

4 pillars of oop

A

Pillar 1: Abstraction
Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components.

Pillar 2: Encapsulation
It is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.

Pillar 3: Inheritence 
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class.

Pillar 4: Polymorphism

It refers to the ability of OOPs programming languages to differentiate between entities with the same name efficiently. This is done by Java with the help of the signature and declaration of these entities.

29
Q

what is a pure function

A

A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. It does not depend on any state or data change during a program’s execution. Rather, it only depends on its input arguments.

30
Q

What is Idempotence and how its different from pure functions

A

A function is said to be idempotent if it returns the same output for the same input or does what we expect it to do. Idempotence is different from pure function as it allows side effects. An example could be calling an API with an input and returning the same output no matter how many times it has been called. Another interesting feature of idempotence is the ability to call itself multiple times and still return the same output.

31
Q

Referential transparency

A

Referential transparency is the ability to replace an expression with its result, without changing the meaning/behavior of the program.

It is a property of expressions, which applies to pure functions, since these functions are essentially parametrized expressions.

32
Q

Partial app vs Function currying

A

tw Currying: Lets you call a function, splitting it in multiple calls, providing one argument per-call. Partial Application: Lets you call a function, splitting it in multiple calls, providing multiple arguments per-call.

33
Q

pipe vs compose functions in js

A

it is a way of chaining multiple functions together to create a new function, in other terms we are solving a problem by reducing it into smaller solutions that in themselves don’t accomplish much but together can solve complex tasks.

34
Q

What is arity in functional programming?

A

the number of arguments or operands taken by a function, operation or relation in logic, mathematics, and computer science.

35
Q

What is composition in functional programming?

A

Function composition is a way of combining pure functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of one function is passed as the argument of the next, and the result of last one is the result of the whole. Composition is a fancy term which means “combining”.

36
Q

OOP vs FP

A

OOP:

  • encapsulation via data composition and data hiding
  • polymorphism via inheritance (usually classical, sometimes prototypical, sometimes other. Many OO languages also have generics, but that’s not part of OO)
  • statusfull
  • impure
  • few operations on common data

FP:

  • pure
  • statelss
  • many operations on fixed data
  • Process composition with first-class function objects.
37
Q

why js is single threaded

A

Because multi-threading is hard, and JS was made for noobs.
JavaScript does not support multi-threading because the JavaScript interpreter in the browser is a single thread (AFAIK). Even Google Chrome will not let a single web page’s JavaScript run concurrently because this would cause massive concurrency issues in existing web pages. All Chrome does is separate multiple components (different tabs, plug-ins, etcetera) into separate processes, but I can’t imagine a single page having more than one JavaScript thread.

38
Q

what is tight coupling?

A

Tight coupling is when a group of classes are highly dependent on one another.