「壹題」git
第 106 題:分別寫出以下代碼的返回值
String('hello') == new String('hello');
String('hello') === new String('hello');
複製代碼
String
全局對象是一個用於字符串或一個字符序列的構造函數。github
new
運算符建立一個用戶定義的對象類型的實例或具備構造函數的內置對象的實例。函數
因此:post
String('hello') === new String('hello'); // false
// 由於
typeof String('hello'); // string
typeof new String('hello'); // object
複製代碼
你所忽略的js隱式轉換 通俗易懂。ui
/* * 首先,x 與 y 類型不一樣,x 爲 string 類型,y 爲對象類型,故先進行原始類型轉換。 * js引擎內部的抽象操做 toPrimitive(input, Number) 先執行 valueof() 方法,返回結果仍爲對象。 * 繼續執行 toString() 方法,返回 'hello'。 */
String('hello') == new String('hello'); // true
複製代碼