How JavaScript works?
JavaScript is a single-threaded language that can be non-blocking.JavaScript Engine
For the code below:
const f()=>{ const ff()=>{ console.log("a"); } } f();
f(), ff()and console.log("a") will be push in the call stack one by one and be popped out in the reverse order (first in last out).
Single threaded means the JavaScript Engine has only one call stack. (simple, multithreaded environment is complicated and has many issues like "deadlock" to deal with.)
Synchronous programming means the program gets executed one line after another in order. If we have one function that takes a lot of time, the whole line will be held up.
When we need to do things like image processing or making requests over the network like API calls, we should do asynchronous programming.
We can do asynchronous programming with setTimeout(), for example:
console.log("1"); setTimeout(() => { console.log("2"); }, 2000); console.log("3"); //>> 1, 3, 2
console.log("1") is pushed in the call stack and executed, then popped out;
setTimeout() was pushed in the call stack. Bc it is not part of JavaScript but part of Web APIs, it triggers the Web APIs, then is popped out of the call stack. And the Web APIs starts a timer of 2 seconds.
During the 2 seconds, as the call stack is empty, console.log("3") is pushed in, executed and pushed out.
After 2 seconds, the Web APIs adds a callback of setTimeout to the callback queue, ready to rune the content inside of setTimeout().
The event loop keeps checking the callback stack and only if the call stack is empty, it will push the first thing in the queue to the stack and do some work. So the callback is removed from the callback queue and added in the call stack, and by running it we add console.log("2") in the call stack, when it is done, the callback is popped out. Now everything is empty.
?? if the time in setTimeout() is set to 0, the output order is still the same, bc only if the call stack is empty, it will push the first thing in the queue to the stack and do some work.
?? JavaScript is synchronous in the sense that it is single-threaded but can have asynchronous code by using the event queue on things like AJAX requests.
S Data structures Data typesThe latest ECMAScript standard defines seven data types (built-in types):
Six primitive data types:
Boolean
Null
Undefine
Number
String
Symbol (new in ECMAScript6)
Object
Array and Function are two specialized version of the Object type. Use typeof operator to figure out the type of the value that held by a variable (only values have types, variables are only containers):
var a; typeof a; // >>"undefined" var b = null; typeof b; // >>"object" var c = undefined; typeof c;// >>"undefined" typeof function() {} === "function"; // >>"True" var arr = [1, 2, 3]; typeof arr; // >>"object" // use Array.isArray or Object.prototype.toString.call // to differentiate regular objects from arrays
?? typeof null returns Object instead of null is a long-standing bug in JS, but one that is likely never going to be fixed. Too much code on the Web relies on the bug and thus fixing it would cause a lot more bugs!
?? The typeof of arrays is object.
?? A variable value can be undefined by 1. explicitly assignment, 2. declared with no assignment, 3. function return with no value, 4. usage of void operator
Coercion Truthy & FalsyA non-boolean value can always be coerced to a boolean value. The specific list of "falsy" values are as follows:
empty string: ""
0, -0
invalid number: NaN
null, undefined
false
Equality & InequalityThere are four equality operators: ==, ===, !=, and !==.
Overall, the difference between == and === is that == checks the equality with coercion allowed, and === checks the equality without coercion allowd ("strictly equality").
check Ecma-262 Edition 5.1 Language Specification here
if: typeof x differs from typeof y, return false
elif: typeof x is "undefine" or "null", return true, else:
elif: typeof x is "number"
if: x or y is NaN, return false (??NaN === nothing)
elif: x is the same number value as y, return true
elif: x is +0, y is -0 and vice versa, return true
else: return false
elif: typeof x is "string"
if: x and y are exactly the same sequence of characters (same length and same characters in corresponding positions), return true
else: return false
elif: typeof x is "boolean"
if: x and y are both "true" or "false", return true
else: return false
elif: typeof x is "object"
if: x and y both refer to the same object, return true
else: retrun false
else: pass
The Abstract Equality Comparison Algorithm (x == y)if: typeof x is the same as typeof y, return the same result as x === y
elif: typeof x is undefined and typeof y is null, and vice versa, return true
elif: typeof x is "string" and typeof y is "number", return the result of ToNumber(x) == y, and vice versa
elif: typeof x is "boolean", return the result of ToNumber(x) == y, and vice versa ??
elif: typeof x is "object", typeof y is "string" / "number", return the result of ToPremitive(x) == y, and vice versa ??
else: return false
Sameness comparison of == and === Relational Comparison文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/98311.html
摘要:對(duì)于有著完全的支持是一個(gè)交互式的開發(fā)環(huán)境,是的下一代產(chǎn)品,集成了更多的功能,等其正式版發(fā)布,相信那時(shí)就是被取代的時(shí)候通過使用,能夠以靈活,集成和可擴(kuò)展的方式處理文檔和活動(dòng)可以開啟終端,用于交互式運(yùn)行代碼,完全支持豐富的輸出支持,,,等任何文 showImg(https://segmentfault.com/img/remote/1460000018602436?w=1282&h=721...
摘要:強(qiáng)烈建議在虛擬環(huán)境下使用安裝,這樣就不需要什么或之類的了,也不會(huì)搞亂系統(tǒng)級(jí)的配置。安裝好后,就可以用命令來執(zhí)行。親測即使是在虛擬環(huán)境中運(yùn)行的,也會(huì)識(shí)別同樣的這個(gè)文件。 強(qiáng)烈建議在Virtualenv虛擬環(huán)境下使用pip安裝,這樣就不需要什么sudo或--user之類的了,也不會(huì)搞亂系統(tǒng)級(jí)的配置。 一鍵安裝所有東西: # 安裝插件配置器 pip install jupyter_nbext...
摘要:原文鏈接這些年,你不能錯(cuò)過的學(xué)習(xí)資源寫在前面本系列是綜合了自己在學(xué)習(xí)過程中的理解記錄對(duì)參考文章中的一些理解個(gè)人實(shí)踐過程中的一些心得而來。 原文鏈接:『 Spark 』5. 這些年,你不能錯(cuò)過的 spark 學(xué)習(xí)資源 寫在前面 本系列是綜合了自己在學(xué)習(xí)spark過程中的理解記錄 + 對(duì)參考文章中的一些理解 + 個(gè)人實(shí)踐spark過程中的一些心得而來。寫這樣一個(gè)系列僅僅是為了梳理個(gè)人學(xué)習(xí)s...
摘要:正則表達(dá)式一個(gè)描述字符模式的對(duì)象正則表達(dá)式的定義構(gòu)造函數(shù)正則表達(dá)式直接量一對(duì)斜杠新特性正則的擴(kuò)展引用類型類型的注意要點(diǎn)用于模式匹配的方法不支持全局搜索忽略表達(dá)式參數(shù)中的修飾符兩個(gè)參數(shù)第一個(gè)是正則表達(dá)式,第二個(gè)是要替換的字符串接收一個(gè)正則表達(dá) 正則表達(dá)式(regular expression):一個(gè)描述字符模式的對(duì)象 1 正則表達(dá)式的定義 RegExp()構(gòu)造函數(shù) 正則表達(dá)式直接量(一...
閱讀 857·2023-04-25 19:53
閱讀 4524·2021-09-22 15:13
閱讀 2721·2019-08-30 10:56
閱讀 1463·2019-08-29 16:27
閱讀 3097·2019-08-29 14:00
閱讀 2573·2019-08-26 13:56
閱讀 732·2019-08-26 13:29
閱讀 1768·2019-08-26 11:31