资讯专栏INFORMATION COLUMN

Advanced JS Notebook

jimhs / 900人阅读

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.

JavaScript Run-time environment

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 types

The 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 & Falsy

A 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 & Inequality

There 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").

The Strict Equality Comparison Algorithm (x === y)

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/98311.html

相关文章

  • JupyterLab:程序员的笔记本神器

    摘要:对于有着完全的支持是一个交互式的开发环境,是的下一代产品,集成了更多的功能,等其正式版发布,相信那时就是被取代的时候通过使用,能够以灵活,集成和可扩展的方式处理文档和活动可以开启终端,用于交互式运行代码,完全支持丰富的输出支持,,,等任何文 showImg(https://segmentfault.com/img/remote/1460000018602436?w=1282&h=721...

    rubyshen 评论0 收藏0
  • Jupyter Notebook 安装插件

    摘要:强烈建议在虚拟环境下使用安装,这样就不需要什么或之类的了,也不会搞乱系统级的配置。安装好后,就可以用命令来执行。亲测即使是在虚拟环境中运行的,也会识别同样的这个文件。 强烈建议在Virtualenv虚拟环境下使用pip安装,这样就不需要什么sudo或--user之类的了,也不会搞乱系统级的配置。 一键安装所有东西: # 安装插件配置器 pip install jupyter_nbext...

    宋华 评论0 收藏0
  • 『 Spark 』5. 这些年,你不能错过的 spark 学习资源

    摘要:原文链接这些年,你不能错过的学习资源写在前面本系列是综合了自己在学习过程中的理解记录对参考文章中的一些理解个人实践过程中的一些心得而来。 原文链接:『 Spark 』5. 这些年,你不能错过的 spark 学习资源 写在前面 本系列是综合了自己在学习spark过程中的理解记录 + 对参考文章中的一些理解 + 个人实践spark过程中的一些心得而来。写这样一个系列仅仅是为了梳理个人学习s...

    mist14 评论0 收藏0
  • JS语言核心——“正则表达式的模式匹配”

    摘要:正则表达式一个描述字符模式的对象正则表达式的定义构造函数正则表达式直接量一对斜杠新特性正则的扩展引用类型类型的注意要点用于模式匹配的方法不支持全局搜索忽略表达式参数中的修饰符两个参数第一个是正则表达式,第二个是要替换的字符串接收一个正则表达 正则表达式(regular expression):一个描述字符模式的对象 1 正则表达式的定义 RegExp()构造函数 正则表达式直接量(一...

    李世赞 评论0 收藏0

发表评论

0条评论

jimhs

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<