JS 冷知識#1 new 可以不加括號你知道嗎?

Dian
Dec 11, 2023
source: DALL·E

某天剛好看到一段被 minify 後的 JS ,出現了奇怪的寫法🤯

{
// some object
plugin:[new User]
....
}

比對一下minify前的 code,原本是有括號的 plugin:[new User()] 怎麼不見了,是對還是不對?

特別去查了一下🔍,還真的可以這樣寫。不過當然絕對不建議平常使用這種寫法。一不小心可能就混到了 IIFE 的功能了。

參考資料如下:
mdn web docs
ESlint rule

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Dian
Dian

Written by Dian

學習>吸收>實作>再學習

No responses yet

Write a response