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文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/98311.html
摘要:對于有著完全的支持是一個交互式的開發環境,是的下一代產品,集成了更多的功能,等其正式版發布,相信那時就是被取代的時候通過使用,能夠以靈活,集成和可擴展的方式處理文檔和活動可以開啟終端,用于交互式運行代碼,完全支持豐富的輸出支持,,,等任何文 showImg(https://segmentfault.com/img/remote/1460000018602436?w=1282&h=721...
摘要:強烈建議在虛擬環境下使用安裝,這樣就不需要什么或之類的了,也不會搞亂系統級的配置。安裝好后,就可以用命令來執行。親測即使是在虛擬環境中運行的,也會識別同樣的這個文件。 強烈建議在Virtualenv虛擬環境下使用pip安裝,這樣就不需要什么sudo或--user之類的了,也不會搞亂系統級的配置。 一鍵安裝所有東西: # 安裝插件配置器 pip install jupyter_nbext...
摘要:原文鏈接這些年,你不能錯過的學習資源寫在前面本系列是綜合了自己在學習過程中的理解記錄對參考文章中的一些理解個人實踐過程中的一些心得而來。 原文鏈接:『 Spark 』5. 這些年,你不能錯過的 spark 學習資源 寫在前面 本系列是綜合了自己在學習spark過程中的理解記錄 + 對參考文章中的一些理解 + 個人實踐spark過程中的一些心得而來。寫這樣一個系列僅僅是為了梳理個人學習s...
摘要:正則表達式一個描述字符模式的對象正則表達式的定義構造函數正則表達式直接量一對斜杠新特性正則的擴展引用類型類型的注意要點用于模式匹配的方法不支持全局搜索忽略表達式參數中的修飾符兩個參數第一個是正則表達式,第二個是要替換的字符串接收一個正則表達 正則表達式(regular expression):一個描述字符模式的對象 1 正則表達式的定義 RegExp()構造函數 正則表達式直接量(一...
閱讀 689·2023-04-25 19:53
閱讀 4262·2021-09-22 15:13
閱讀 2565·2019-08-30 10:56
閱讀 1320·2019-08-29 16:27
閱讀 2932·2019-08-29 14:00
閱讀 2407·2019-08-26 13:56
閱讀 426·2019-08-26 13:29
閱讀 1611·2019-08-26 11:31