JavaScript内存空间-原始类型与引用类型

分类:

1.原始类型:数值、字符串、布尔、null、undefined

2.引用类型:对象(Array、Date、Math。。)

原始类型与引用类型三种情况下的对比:

1.赋值

//原始类型赋值
let str1 = "hello";
let str2 = str1;
str1 = "world";
console.log(str1);//world
console.log(str2);//hello

//引用类型赋值
let stu1 = {name:"xiaoming"};
let stu2 = stu1;
stu1.name = "xiaohong";
console.log(stu1.name);//xiaohong
console.log(stu2.name);//xiaohong

原始类型赋【值】,引用类型赋【引用】

2.比较

//原始类型的比较 ===
let str1 ="hello"
let str2 ="hello"
console.log(str1 === str2)//true

//引用类型的比较
let stu1 = {name:"xiaoming"}
let stu2 = {name:"xiaoming"}
let stu3 = stu1
console.log(stu1 === stu2)//false
console.log(stu1 === stu3)//true

 原始类型比较的是值是否相等,引用类型比较的是【引用】是否指向同一个对象。

4.函数传参

 

//原始类型传参
function fun(num){
    num = 100
}
let n = 10
fun(n)
console.log(n)//10

//引用类型传参
function fun1(arr){
    arr.push(10)
}
let a = [1,2,3]
fun1(a)
console.lgo(a)//[1,2,3,10]

原始类型作为参数,函数内的操作不影响实参的值。引用类型作为参数,函数内的操作会影响实参的值。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值