你可能不知道在JS世界裡的特殊物件
特殊物件清單
JavaScript是一個有著龐大使用族群的程式語言,但是因為其歷史淵源和不同考量等因素下,其中有不少令人萬丈摸不著頭緒的設計。自連class
都只作為保留字而無實際作用的時候,就已經有在接觸,在後續越了解越多,想想應該是能來分享一些,其中一些我知道的特殊物件。
undefined
null
this
super
NaN
Infinity
new
new.target
Object.prototype
- 先有
Function
還是先有Object
- 先有
Symbol
Symbol.for()
、Symbol.keyFor()
document.all
typeof document.all
arguments
hashbang
- HTML comment
'use strict'
globalThis
window
document
其中有一些並不是真正的物件,但都是一些執行環境下支援特殊寫法。或許有一些並沒有實際作用,但可能很多人並不知道,畢竟平常大概也沒有人會這樣寫吧!所以其實也就是一些JavaScript裡無關緊要的有趣小地方。
當然…當中有一部分也有可能成為你日後會踩入的陷阱(抗)。那麼就先來說說undefined
和null
吧!
undefined
undefined
是一個屬於undefined
的物件。(但可能不是唯一)
typeof undefined; // -> "undefined"
In all non-legacy browsers, undefined is a non-configurable, non-writable property. (Even when this is not the case, avoid overriding it.) – from MDN
儘管在現今主流的瀏覽器都是不可改變全域的undefined
變數: