ECMAScript 之 Set Type
ECMAScript 2015 亦提供 Set,與 Dart 的 Set 相同,只是語法不太一樣。
失くすものさえない今が強くなるチャンスよ
ECMAScript 2015 亦提供 Set,與 Dart 的 Set 相同,只是語法不太一樣。
SQL 的 LIKE 非常好用,若要在 JavaScript 實現 LIKE,則要搭配 filter()
與 includes()
。
當使用只有 年日
的 日期套件
時,當轉成 Timestamp 時是以 00:00:00
,可能造成後端時間區的錯誤,此時可將時間拉滿成 23:59:59
。
雖然可以從 Server 端直接處理 CSV 下載,但若 CSV 內必須處理多國語言,則必須將 Object Array 先下載到 Browser,然後再轉成 CSV 下載。
實務上 API 常回傳帶有 TZ 的 ISO String 日期,可使用 Date.parse()
將 ISO String 轉成 ECMAScript 的 Timestamp。
JavaScript 使用 Unicode 之後,無論是 中文
或 英文
都視為一個字元,若想將中文
視為 兩個字元
,英文
視為 一個字元
,則無法單純使用 ECMAScript 內建的 length()
。
可將 Object 當成簡易的資料庫,當 key 重複時,會自動將其覆蓋,不需做特別處理,非常方便。
實務上可能遇到 API 讀取錯誤,需自行重試 N 次讀取才能成功。
實務上可能遇到 URL 已經發布,但 URL 尚無法使用,需自行重試 N 次等 URL 存在再繼續。
若我們想將 Object 直接解構成變數,若變數不多,可直接使用 Object Destructuring,但若變數很多,可改用 eval()
動態解構出多個變數。
If we want to pad the partial string, we can’t simply use padStart()
to get the result.
If we want to substring to a specific character, we can’t just use substring()
to get the result.
Sometimes we may get an Object with an Array on the parameter. We can use Object and Array destructuring together for the function parameter.
Sometimes we may get data by the data structure of a Recursive Object. We have to write some recursive functions to find value with these Recursive Objects.
If data is an Object, we can use Object.entries
, map
and join
to convert Object into Query String.
As for extracting Floating-point Number to finite digit, we can use parseInt
or toFixed
.
We can convert from Number to String by String
, toString
, Type Coercion or Template String.
We can convert from String to Number by Number
, parseInt
, +
or ~~
operator.
We often use Number Literal in Practical, not Wrapper Object of Number. We can call instance method directly on Number Literal. But why?
In addition to using Primitive for Number, we can use Primitive Wrapper Object to wrap Number.
We can define integer literal in decimal, hexadecimal, octal or binary.
If we want to add invisible properties to the Object, we can use Symbol Property to make properties invisible.
If we want to add invisible methods to the Object, we can use Symbol Method to make methods invisible.
console.log
、console.warn
and console.error
can display debugging messages on the console, but console.error
is suitable for displaying runtime error.
Although we can fully customize console.log
with CSS inline style, console.warn
provides a simple way to make debugging messages a little different from console.log
.
If we use console.log
to display the DOM element, we will get pure HTML only. What we have to do is using console.dir
to get the property of the DOM element.
If we care about the performance of some code snippets, we can use console.time
to measure the performance.
If we use console.log
in the for
loop, it may produce too many debugging messages to read. It’s a good time to use the console.group
to group them under a group.
Besides using console.log
to display debugging messages, we can use console.table
to display Array of Objects for a better layout.
Not only just showing debugging messages on the console of DevTools, but we can also use CSS to change their style.
If we want to add Object property at runtime, it chanced that we override property of original Object. Since Symbol is a unique value, using Symbol Property to prevent property from overriding.
If we only allow limited values for arguments, using Enums is a good choice. Unfortunately, we don’t have native Enums in JavaScript, but we can use Object and Symbol to simulate it.
Symbol is a new type for unique value in ECMAScript 2015.
ECMAScript 2015 introduced Array.prototype.entries
, which returns index and element from Array.
If we want to get key and value from Object simultaneously, we used to use Object.keys
to get all keys, and then use []
with the key to get value. Now we can use Object.entries
to get key and value at once.
Object Destructuring was introduced by ECMAScript 2015. Using it with function argument, we can implement Pass By Name
feature in ECMAScript.
當 ECMAScript 2015 引進 Promise 概念後,實務上會遇到 Array Promise,若要搭配 ECMAScript 2017 的 async await
,可使用 for await of
讓 Codebase 更精簡。
實務上較少刪除 Object 的 Property,但若真的想刪除 Property,ECMAScript 提供了兩種方法。
若想取得 Object 的 Property 做運算,但又不確定 Object 是否包含此 Property 時,ECMAScript 提供多種方式可判斷 Property 是否存在。
Array.prototype.reduce
只能接受傳入 Synchronous Function,若要傳入 Asynchronous Function,可依需求使用不同方式實現。