JavaScript红宝书第5章:基本引用类型

引用类型是什么?

引用类型又称复杂数据类型,展开就是某个特定引用类型的实例。实例又称为对象,对象通过new操作符后跟一个构造函数来创建,构造函数就是用来创建新对象的函数,例:

let obj=new Object();//普通引用类型
let dat=new Date();//日期引用类型
let reg=new RegExp(/hello/);//正则引用类型
let boo=new Boolean(true);//布尔特殊引用类型
let str=new String('string');//字符特殊引用类型
let num=new Number(100);//数字特殊引用类型

接下来我们展开讲解一下这几个引用类型是语法格式,使用场景,和需要注意的部分。

Date

Date类型早期是参考了Java的java.util.Date,它可以表示一个具体的日期。
创建日期对象:

let dat=new Date();//Thu Sep 28 2023 10:10:52 GMT+0800 (中国标准时间)

常用方法

Date.parse()

这个方法主要作用就是用来判断输入的内容来转换Date对象,比如:

let dat1=new Date(Date.parse("5/23/1990"))//月/日/年
let dat2=new Date(Date.parse("May 5,2019"))//月名 日,年
let dat3=new Date(Date.parse("Tue May 23 2019 00:00:00 GMT-0700"))
//周几 月名 日 年 时:分:秒 时区

同时如果传给Date.parse的不是一个日期,那么就会返回一个Invalid Date 翻译就是无效的日期。

Date.UTC()

UTC和parse返回的参数相同,但是UTC中输入的参数只有年和月是必需的,剩余的可以加也可以不加,月如果是0的话,默认为1。举例:

let dat=new Date(Date.UTC(2000,0))
console.log(dat);
Date.now()

Date.now()就是返回当前时间,举个例子,我们想知道这个函数从开始到结束,需要用到多长时间,可以用Date.now得到这个值。

function Times(){
	return 0;
}
let start=Date.now()
Times()
let stop=Date.now()
let ans=stop-start
console.log(stop-start);

因为这个函数比较简单,所以占用时间甚至不超过1毫秒,所以stop-start等于0.但如果是较难的就会发现。

剩余Date方法简述

toLocalleString和toString方法

toString返回的是包含时区的日期和时间,而toLocalString会返回于浏览器运行的本地环境一致的日期和时间,不包含日期和时间。

getTime、getFullYear、getDate、getMonth方法

getTime和valueof相等,返回一个Date对象的毫秒数。
getFullYear返回年,getDate返回日,getMonth返回月。

const dat = new Date();
const date=dat.getFullYear()+'-'+dat.getMonth()+'-'+dat.getDate()
console.log(date); // 2023-8-28
// Expected output: 8

getMonth因为是从0算起,所以它默认需要进行一个+1操作,才能实现正确数据,同时这个问题是Js存在的问题。

RegExp

正则 与字符串方法search replace split match

ES通过RegExp类型支持正则表达式,创建语法格式:

let reg=/pattern/flags;
let reg=new RegExp(/pattern/)

就比如我们想校验某个字符串里面hello在什么位置,可以调用search方法和正则表达式实现。

let a='abcdhello'
console.log(a.search(/hello/))//4

如果想替换这个字符,可以用replace加这个正则表达式

let a='adhellofafdhello'
while(a.search(/hello/)!=-1){
	a=a.replace(/hello/,'Tom')
}
console.log(a);//adTomfafdTom

在上述代码中,我们想实现替换的操作,search+while进行查询是否还有hello这个字符串,没有search就返回-1。用replace方法实现了hello替换。
也可以通过全局替换/g来进行一个实现。

console.log(reg.replace(/hello/g,'Tom'));

利用split+正则可以实现去除字符和切割的效果
举例:想要去掉某个字符串的所有d字符,后重新拼接在一起。

let reg='abchelloasdasdhelloasd'
let temp=reg.split(/d/)
let ans=''
for(let i=0;i<temp.length;i++){
	ans+=temp[i]
}
console.log(ans);//abchelloasashelloas

在上述代码中,有reg作为字符串变量,我们通过调用split+正则,去把d进行一个切割,然后又用ans+for进行一个拼接操作。
那如果我们想知道这个字符串中到底有多少个d,就可以通过match方法进行查询。

console.log('abchelloasdasdhelloasd'.match(/hello/g).length);//2

正则方法 test exec

let a=new RegExp(/hello/)
console.log(a.test('abchello'));//true
console.log(a.exec('abcdhello'));//['hello', index: 4, input: 'abcdhello', groups: undefined]

在上述代码中,我们首先调用了这个test,test主要是用于判断,这个字符串有没有正则表达式里面的子字符串,有就true,没有就false。
exec方法主要是用于提取第一个正则表达式中的值和所在索引,同时也会返回它的初始值。但是不管这个正则表达式有没有加上全局属性/g,它都只会返回第一个属性值和索引值。多出来的不会返回。

包装对象(Boolean、Number、String)

那么好,在基本数据类型中,有这三种数据类型,可以被new出来,那new出来的数据类型,就变成了包装对象类型,它们也可以调用对应的方法进行使用。同时通过new出来的这三个属性可以被添加新的属性和方法,这也被称为特殊引用类型。

let str1='ddd'
let str2=new String()
str2.color='red'
str1.color='blue'
console.log(str2,str1);//String {'', color: 'red'} 'ddd'
console.log(str2.color,str1.color);//red undefined

在上述代码中可以发现,没有new出来的原始值是无法进行添加属性方法的操作的,new出来的就可以实现,new出来的就变成了包装对象类型也叫特殊引用类型。

这些new出来的特殊引用类型,需要在它们的构造函数里面进行初始值填写,也就是()里面,就比如let boo=new Boolean(true).接下来我们就对这三个特殊引用对象,进行一一的讲解。

Boolean

语法格式:

let boo=new Boolean()

因为所有对象在布尔表达式当中都为true,作为特殊引用类型的Boolean类型也不例外,所以就会出现这样滑稽的一幕。

let boo=new Boolean(false)
let result=boo&&true
console.log(result);//true

除了布尔表达式有问题之外,Boolean实例会重写valueOf方法,toString方法被调用也会被覆盖,同时在进行类型判断的时候大多数的判断结果都是object而不是boolean,所以不建议使用Boolean对象来进行开发。

Number

语法格式

let num=new Number(10)

Number也重写了toString、valueOf方法,它的参数值,主要是用于进制的转换。

let num=new Number(16)
console.log(num.toString(2));//1000
console.log(num.toString(8));//20
console.log(num.toString(16));//10
console.log(num.toString(7));//22
console.log(num,num.valueOf());//Number {16} 16

如果想将数字转换成字符串,number对象类型也提供了toFixed方法,这个方法不仅能转换,还能包含指定小数点位数的数值字符串。

console.log(num.toFixed(10),typeof(num.toFixed(10)));//16.0000000000 string

它还有toExponential,toPrecisionisInteger方法等等,不常用,这里就不赘述了。

String

语法格式:

let str=new String('content')
简述valueOf,toString,toLocalSting,charAt,concat,slice,substr,substring

valueOf,toString,toLocalString这三个方法都能够返回String对象的原始值。
同时它也有length属性,用于判断它当前的长度。可以利用charAt方法来进行输入索引输出索引下的值。js字符串是由16位码元组成。通过charCodeAt方法可以查看指定码元下.拼接字符串用到concat,同时concat可以一次拼接多个字符串,举例

let str=new String('hello')
str=str.concat('world',str)
console.log(str);//helloworldhello

同时想从字符串中提取子字符串可以通过三个方法实现:slice,substr,substring,区别在于,举例说明:

let str=new String('hello')
console.log(str.substr(-1));//o
console.log(str.slice(-1));//o
console.log(str.substring(-1));//hello

substr和slice能转换负数为倒数,而substring会把负数转换成0,输出全部字符串。

再次举例说明如果第二个参数是负数它们之间会发生什么区别

let str=new String('hello world')
console.log(str.substr(3,-4));//''
console.log(str.slice(3,-4));//'lo w'
console.log(str.substring(3,-4));//hel

slice方法将-4转换为7,substring方法转换为0,substring会将两个参数大小作为起点和终点的判断,而substr方法会将第二个参数转换为0,也就是说获取0个字符,返回空。

includes indexOf repeat toLowerCase toUpperCase

includes判断某个字符是否存在于另一个字符。repeat是指可以将该字符串复制多少次,toLowerCase、toUpperCase是指大小写字母。举例说明:

let str='hello World'
let str1='hello'
console.log(str.includes(str1));//true
console.log(str.indexOf(str1),str.indexOf('1'));//0 -1
console.log(str.repeat(2));//hello Worldhello World
console.log(str.toUpperCase(),str.toLowerCase());//HELLO WORLD hello world

常用的还有match,search方法,在正则讲过,这里就不赘述了。

单例内置对象Global

浏览器将window对象实现为Global的代理,所有在全局声明的变量都是window变量。

Math对象

常用方法:min,max,random,ceil(向上取整),floor(向下取整),abs(正数)

当代码开始执行时,全局上下文会存在Global和Math这两个内置对象。前者在浏览器中用window代理,后者主要用于复杂的数学运算。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃巧克li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值