在JavaScript(简称JS)中,一切皆为对象,这种说法在某种程度上是成立的,但也需要具体分析。以下是对“JS是否为对象”这一问题的详细解答:
number
、string
、boolean
、null
、undefined
、symbol
(ES6新增)和bigint
(ES10新增)。object
,包括普通对象、数组、函数等。null
被错误地表示为对象(typeof null === 'object'
),但它实际上是一个特殊的原始值,表示“无”或“空”。number
、string
、boolean
等也不是对象。new
关键字或字面量方式创建。// 原始类型示例
let num = 10;
let str = "hello";
let bool = true;
console.log(typeof num); // "number"
console.log(typeof str); // "string"
console.log(typeof bool); // "boolean"
console.log(typeof null); // "object"(这是一个历史遗留问题)
// 引用类型示例
let obj = { name: "Alice", age: 25 };
let arr = [1, 2, 3];
let func = function() { return "Hello World"; };
console.log(typeof obj); // "object"
console.log(typeof arr); // "object"
console.log(typeof func); // "function"(函数也是对象的一种特殊形式)
领取专属 10元无门槛券
手把手带您无忧上云