Zerotomastery JS advanced Flashcards
Which engine google chrome uses ?
V8 developed by google. Also used by node js
Which language V8 is written ?
C++
is JS interpreted language ?
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
methods, and things which are bad for optimization
- eval()
- arguments
- for in
- with
- delete
bad for inline caching and hidden classes
what is inline caching and give me then example
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
What is heap memory
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.
What is call stack
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.
What is garbage collection in js
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.
what is a memory leak
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.
What is js runtime and web APIS
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.
Common ways to cause memory leak
Global variables
EventListeners and not removing them
Setinterval
What is exacution context ?
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.
What is lexical environment?
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
What is scope
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.
What is a scope chain
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.
Why are global variables considered bad practice?
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.
what is THIS keyword
This is a object which funciton is property of
Context vs Scope
Scope pertains to the visibility of the variables, and context refers to the object within which a function is executed
What is a closure ?
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
What is encapsulation?
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.
Prototypal Inheritance
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.
proto vs prototype
__proto__ is an object in every class instance that points to the prototype it was created from.
Which one is dynamically scoped and lexically scoped
arrow function or regular function
Regular function is dynamically scoped. It changes this keyword based on where its been called
Does JS have classes?
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.