javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > js 基本类型与引用类型区别

JavaScript基本类型与引用类型区别解析

作者:指尖跳动的光

文章介绍了JavaScript中基本类型和引用类型的区别,包括它们的存储位置、赋值行为、比较方式、可变性、类型数量、方法支持等特性,同时,还讨论了基本类型和引用类型在函数参数传递、深拷贝与浅拷贝、性能优化以及检测方法等方面的区别和注意事项,感兴趣的朋友一起看看吧

1. 基本类型(Primitive Types)

1.1 特点

1.2 示例

let a = 10;
let b = a; // 赋值时复制实际值
b = 20;    // 修改 b 不影响 a
console.log(a); // 10
console.log(b); // 20

1.3 关键行为

2. 引用类型(Reference Types)

2.1 特点

2.2 示例

let obj1 = { name: "Alice" };
let obj2 = obj1; // 赋值时复制引用(地址)
obj2.name = "Bob"; // 修改 obj2 会影响 obj1
console.log(obj1.name); // "Bob"
console.log(obj2.name); // "Bob"

2.3 关键行为

3. 核心区别总结

特性基本类型引用类型
存储位置栈内存(直接存储值)堆内存(存储地址,变量保存引用)
赋值行为复制实际值(独立)复制引用(共享同一对象)
比较方式比较值是否相等比较引用地址是否相同
可变性不可变(修改创建新值)可变(直接修改原对象)
类型数量固定 7 种动态(所有对象类型)
方法支持临时转换为对象调用方法(原值不变)直接调用方法修改原对象

4. 常见场景与注意事项

4.1 函数参数传递

4.2 深拷贝 vs 浅拷贝

4.3 性能优化

5. 检测方法

5.1 基本数据类型-》检测类型

  1. typeof 运算符
    • 适用于大多数基本类型,但 null 会返回 “object”(历史遗留问题)。
      typeof "hello"; // "string"
      typeof 42; // "number"
      typeof true; // "boolean"
      typeof undefined; // "undefined"
      typeof Symbol("foo"); // "symbol"
      typeof 10n; // "bigint"
      typeof null; // "object" ⚠️ 特殊情况
      
  2. 严格等于 === 检测 null 和 undefined
    let x = null;
    let y = undefined;
    console.log(x === null); // true
    console.log(y === undefined); // true
    
  3. Object.prototype.toString.call()(最准确)
    Object.prototype.toString.call("hello"); // "[object String]"
    Object.prototype.toString.call(42); // "[object Number]"
    Object.prototype.toString.call(true); // "[object Boolean]"
    Object.prototype.toString.call(null); // "[object Null]"
    Object.prototype.toString.call(undefined); // "[object Undefined]"
    Object.prototype.toString.call(Symbol("foo")); // "[object Symbol]"
    Object.prototype.toString.call(10n); // "[object BigInt]"
    

5.2 引用数据类型-》检测类型

  1. typeof 运算符
    • typeof 对引用类型通常返回 “object”(除了 function):
      typeof {}; // "object"
      typeof []; // "object"
      typeof function() {}; // "function" ⚠️ 特殊情况
      typeof new Date(); // "object"
      typeof /regex/; // "object"
      
  2. instanceof 运算符
    • 检测对象是否是某个构造函数的实例:
      [] instanceof Array; // true
      {} instanceof Object; // true
      function() {} instanceof Function; // true
      new Date() instanceof Date; // true
      
    • 局限性:
      • 不能检测基本类型(如 “str” instanceof String 返回 false)。
      • 跨框架(iframe)时可能不准确(不同全局对象的构造函数不同)。
  3. constructor 属性
    • 返回对象的构造函数:
      "hello".constructor === String; // true
      (42).constructor === Number; // true
      [].constructor === Array; // true
      ({}).constructor === Object; // true
      
    • 局限性:
      • 如果对象是 null 或 undefined,会报错(Cannot read property ‘constructor’ of null)。
      • 可以被手动修改(obj.constructor = Foo),不可靠。
  4. Object.prototype.toString.call()(最准确)
    Object.prototype.toString.call([]); // "[object Array]"
    Object.prototype.toString.call({}); // "[object Object]"
    Object.prototype.toString.call(function() {}); // "[object Function]"
    Object.prototype.toString.call(new Date()); // "[object Date]"
    Object.prototype.toString.call(/regex/); // "[object RegExp]"
    

5.3 综合检测方法

  1. 判断是否为基本类型
    function isPrimitive(value) {
      return (
        value === null ||
        value === undefined ||
        typeof value === "string" ||
        typeof value === "number" ||
        typeof value === "boolean" ||
        typeof value === "symbol" ||
        typeof value === "bigint"
      );
    }
    
  2. 判断具体类型(推荐 Object.prototype.toString)
    function getType(value) {
      return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
    }
    getType("hello"); // "string"
    getType(42); // "number"
    getType([]); // "array"
    getType({}); // "object"
    getType(null); // "null"
    getType(undefined); // "undefined"
    getType(function() {}); // "function"
    getType(new Date()); // "date"
  3. 判断是否为数组
    Array.isArray([]); // true
    [] instanceof Array; // true
    Object.prototype.toString.call([]) === "[object Array]"; // true
    
  4. 判断是否为 null 或 undefined
    function isNullOrUndefined(value) {
      return value === null || value === undefined;
    }
    

5.4 检测类型总结

检测方式基本类型引用类型特殊情况
typeof✅(除 null)⚠️(function 除外)null 返回 “object”
instanceof跨框架不准确
constructor❌(除 new String() 等)可被修改,null/undefined 报错
Object.prototype.toString.call()最准确

到此这篇关于JavaScript基本类型与引用类型有什么区别的文章就介绍到这了,更多相关js 基本类型与引用类型区别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文