Object() 构造函数
Object() 构造函数将输入转换为一个对象。它的行为取决于输入的类型。
语法
参数
value-
任意值。
返回值
示例
创建一个新对象
js
const o = new Object();
o.foo = 42;
console.log(o);
// { foo: 42 }
使用 undefined 和 null 类型调用 Object
以下示例在 o 中存储一个空 Object 对象:
js
const o = new Object();
js
const o = new Object(undefined);
js
const o = new Object(null);
获取 BigInt 和 Symbol 的封装对象
当用 new 调用 BigInt() 和 Symbol() 构造函数时会抛出一个错误,以阻止创建封装对象而不是基本类型值的常见错误。为这些类型创建封装对象的唯一方法是使用它们调用 Object():
js
const numberObj = new Number(1);
console.log(typeof numberObj); // "object"
const bigintObj = Object(1n);
console.log(typeof bigintObj); // "object"
const symbolObj = Object(Symbol("foo"));
console.log(typeof symbolObj); // "object"
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-object-constructor |
浏览器兼容性
BCD tables only load in the browser