// JavaScript 类型示例
// 1. 基本类型 (Primitive Types)
let num = 42; // 数字类型 (Number)
let str = "Hello, World!"; // 字符串类型 (String)
let bool = true; // 布尔类型 (Boolean)
let n = null; // Null 类型
let u = undefined; // Undefined 类型
let sym = Symbol("symbol"); // Symbol 类型
// 2. 引用类型 (Reference Types)
let obj = { name: "Alice" }; // 对象类型 (Object)
let arr = [1, 2, 3]; // 数组类型 (Array), 数组也是对象的一种
let func = function() { console.log("This is a function"); }; // 函数类型 (Function), 函数也是对象的一种
// 3. 检查类型的方法
console.log(typeof num); // "number"
console.log(typeof str); // "string"
console.log(typeof bool); // "boolean"
console.log(typeof n); // "object" (这是一个历史遗留问题,null 被认为是对象)
console.log(typeof u); // "undefined"
console.log(typeof sym); // "symbol"
console.log(typeof obj); // "object"
console.log(typeof arr); // "object" (数组也是对象)
console.log(typeof func); // "function"
// 使用 instanceof 检查引用类型
console.log(arr instanceof Array); // true
console.log(func instanceof Function); // true
这段代码展示了 JavaScript 中的基本类型和引用类型的定义及其检查方法。
上一篇:js closest
下一篇:js 或者
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站