String('hello') | new String('hello')

「壹题」git

第 106 题:分别写出以下代码的返回值
String('hello') == new String('hello');
String('hello') === new String('hello');
复制代码

区分 String 和 new String

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
复制代码

相关文章

相关文章
相关标签/搜索