177. How Javascript works
6699 ワード
How Does Javascript work?
Imagine getting this question during an interview.
Explain the difference between asynchronous and synchronous in Javascript.
Answer>Javascriptは単一スレッド言語としてブロックしなくてもよい(JavaScriptは非ブロックの単一スレッド言語である).
what is a program?
//these are global variables.
const a = 1;
const b = 10;
const c = 100;
With all memory we have limited amount, that we can actually have.Memory Leaks
Memory leaks happen when you have unused memory.
Global variables are bad because there're unused memory.
call stack
console.log('1');
console.log('2');
console.log('3');
The call stack, if you remember, that's what it reads and executes our scripts.
and it gets put in the call stack. JS engine says 'oop!, console.log has been added. Let's pop it onto this call stack.
const one = () => {
const two =() => {
console.log('4')
}
two();
}
What happened here according to the Call Stack?
=> print out number 4
: executed and removed from the callstack.
(on top of the callstack)
And the call stack is now empty.
Answer>Javascriptは単一スレッド言語としてブロックしなくてもよい(JavaScriptは非ブロックの単一スレッド言語である).
=> you can only do one thing at a time.
=> Whatever's at the top of the call stack gets run first then below that, below that, below that.
Why was javascript designed to be single thread?
Single thread can be quite easy, since you don't have to deal with complicated scenarios that arise in multithreaded environment.
you know the site, stackoverflow.
Stack Over Flow means that a stack is overflowing.
Overflow happens when the call stack just gets bigger and bigger until it just doesn't have enough space anymore.
How can we makes over flow?
function foo() {
foo()
}
It just keeps looping over and over, having recursion but there's no end in sight.We keep adding foo to the call stack.
And we have a stack overflow.
Problem: What if line two was a big task we needed to do?
console.log('1');
...
massive work : reallllly massive work.
...
console.log('3'); // take a long time to get logged.
The website would freeze until the task is done and the user just wait there.Synchronous task: if we have one function that takes up a lot of time, it's going to hold up.
We need "nonblocking".
Javascript is a single threaded language that can be non-blocking.
Synchronous execution is great because it's predictable but it can get slow in tasks like image processing, making requests over the network like API calls.
We need something more that just synchronous tasks.
Then How we do asynchronous programming?
console.log('1');
setTimeout(() => {
console.log('2');
}, 2000) // first parameter is the function that we want to run
// second parameter is how many milli seconds we want to wait.
console.log('3');
'console.log(2)' is done 2 seconds later.
//CALL STACK
//WEB API
//CALLBACK QUEUE
//EVENT LOOP
177.15:29参照
What happend if the time is 2sec?
console.log('1');
setTimeout(() => {
console.log('2');
}, 0)
console.log('3');
execute consolelog3 first than 2
Issue of synchronous
Let's recap.
If you wanted to load your latest tweets onto a web page and you do this synchronously, then visitors to your website won't be able to do anything until those tweets are loaded.
This could cause a long delay they even get to see the content of his site.
They may not be able to click anywhere and the page will feel like it's frozen.
Do you remember eventListner?
element.addEventListener('click', () => {
console.log('click')
})
Every time a click happens on the web page, on the DOM we run the callback function, which is console.log('click').When is asynchronous happening?
when you try and talk between machines like speaking to a database making network requests, image processing, reading files...
summary
key questions
Reference
この問題について(177. How Javascript works), 我々は、より多くの情報をここで見つけました https://velog.io/@bianjy/177.-How-Javascript-worksテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol