javascript引用类型

1.基本引用类型

  • Date
let t1=Date.now();//1612360420268
let t2=new Date(2021,1,3,21,58,0);//Wed Feb 03 2021 21:58:00 GMT+0800 (中国标准时间)
console.log(t2.getTime());//1612360680000 (返回日期的毫秒表示)
console.log(t2.setTime(1612360680000));//1612360680000 (设置日期的毫秒表示)
console.log(t2.getFullYear());//2021 (返回4位年数)
console.log(t2.setFullYear(2021));//1612360680000 (设置日期的年)
console.log(t2.getMonth());//1 返回日期的月(0表示1月,11表示12月)
console.log(t2.setMonth(1));//1612360680000 设置日期的月
console.log(t2.getDate());//3 返回日期中的日(1-31)
console.log(t2.setDate(3));//1612360680000  设置日期中的日
console.log(t2.getDay());//3 返回日期中表示周几的数值(0表示周日,6表示周六)
console.log(t2.getHours());//21 返回日期中的时(0-23)
console.log(t2.setHours(21));//1612360680000  设置日期中的时
console.log(t2.getMinutes());//58 返回日期中的分(0-59)
console.log(t2.setMinutes(58));//1612360680000  设置日期中的分
console.log(t2.getSeconds());//0 返回日期中的秒(0-59)
console.log(t2.setSeconds(0));//1612360680000  设置日期中的秒
console.log(t2.getMilliseconds());//0 返回日期中的毫秒
console.log(t2.setMilliseconds(0));//1612360680000  设置日期中的毫秒
  • RegExp
/**
匹配模式标记:
g:全局模式,表示查找字符串的全部内容,而不是找到第一个匹配的内容就结束。
i:不区分大小写。
m:多行模式,表示查找到一行文本末尾时会继续查。
y:粘附模式,表示只查找从lastIndex开始之后的字符串。
u:Unicode模式,启用Unicode匹配。
s:dotAll模式,表示元字符(.)匹配任何字符(包括\n或\r)
**/
let text="cat, bat, \n sat, fat";
let pattern=/.at/g;
let matches=pattern.exec(text);
console.log(matches.index);//0
console.log(matches[0]);//cat
console.log(pattern.lastIndex);//3
matches=pattern.exec(text);
console.log(matches.index);//5
console.log(matches[0]);//bat
console.log(pattern.lastIndex);//8
matches=pattern.exec(text);
console.log(matches.index);//12
console.log(matches[0]);//sat
console.log(pattern.lastIndex);//15

console.log(pattern.test(text));//true
  • Boolean
  • Number
let num=50.00001;
console.log(num.toFixed(2));//"50.00"
console.log(num.toExponential(2));//"5.00e+1"
console.log(num.toPrecision(2));//"50"
console.log(num.toPrecision(1));//"5e+1"

console.log(Number.isInteger(1));//true
console.log(Number.isInteger(1.00));//true
console.log(Number.isInteger(1.01));//false
  • String
let message="a😊";

console.log(message.length);//3
console.log(message.charAt(1));//�
console.log(message.charAt(2));//�
console.log(message.charCodeAt(1));//55357
console.log(message.charCodeAt(2));//56842
console.log(message.codePointAt(1));//128522
console.log(message.codePointAt(2));//56842
console.log(String.fromCharCode(55357,56842));//😊
console.log(String.fromCodePoint(55357,56842));//😊

let str="hello world";
console.log(str.concat("!!"));//"hello world!!"
console.log(str.slice(3,7));//"lo w"
console.log(str.substring(3,7));//"lo w"
console.log(str.substr(3,7));//"lo worl"
console.log(str.slice(3,-4));//"lo w"
console.log(str.substring(3,-4));//"hel"
console.log(str.substr(3,-4));//""

console.log(str.indexOf("o"));//4
console.log(str.lastIndexOf("o"));//7
console.log(str.indexOf("o",6));//7
console.log(str.lastIndexOf("o",6));//4

console.log(str.startsWith("hello"));//true
console.log(str.endsWith("world"));//true
console.log(str.includes("o w"));//true

console.log("  hello word  ".trim());//"hello world"
console.log("ha ".repeat(3));//"ha ha ha "

console.log('foo'.padStart(6));//"   foo"
console.log('foo'.padEnd(6,'.'));//"foo..."

console.log(str.toLowerCase());//"hello word"
console.log(str.toUpperCase());//"HELLO WORD"

let text="cat, bat, sat, fat";
let matches=text.match(/.at/);
console.log(matches.index);//0
console.log(matches[0]);//"cat"
console.log(text.search(/at/));//1
console.log(text.replace(/at/g,"ond"));//"cond, bond, sond, fond"
console.log(text.replace(/(.at)/g,"word($1)"));//"word(cat), word(bat), word(sat), word(fat)"
  • Global
let uri="http://www.wrox.com/illegal value.js#start";
console.log(encodeURI(uri));//"http://www.wrox.com/illegal%20value.js#start"
console.log(encodeURIComponent(uri));//"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start"
eval("console.log(uri)");//"http://www.wrox.com/illegal value.js#start"
  • Math
console.log(Math.max(3,54,32,16));//54
console.log(Math.min(3,54,32,16));//3
console.log(Math.ceil(25.1));//26(向上舍入)
console.log(Math.floor(25.9));//25(向下舍入)
console.log(Math.round(25.1));//25(四舍五入)
console.log(Math.round(25.9));//26(四舍五入)

2.集合引用类型

  • Object
  • Array
let a=[1,2,3,4];
console.log(Array.from(a,function(x){
	return x**this.exponent
},{exponent:2}));//[1,4,9,16]
console.log(Array.of(1,2,3,4));//[1,2,3,4]

let zeros=[0,0,0,0,0];
console.log(zeros.fill(7,1,4));//[0,7,7,7,0]
console.log(a.copyWithin(1,0,2));//[1,1,2,4]
console.log(a.toString());//"1,1,2,4"
console.log(a.join('||'));//"1||1||2||4"

let colors=[];
colors.push("red","green");//["red","green"]
colors.unshift("black","brown");//["black","brown","red","green"]
colors.pop();//["black","brown","red"]
colors.shift();//["brown","red"]
colors.concat(["red","green"]);//["brown","red","red","green"]
colors.splice(2,1,"yellow");//["brown","red","yellow","green"]

let values=[0,1,5,10,15]
console.log(values.sort());//[0, 1, 10, 15, 5] (默认比较字符串)
console.log(values.sort((v1,v2) => v2-v1));//[15, 10, 5, 1, 0]
console.log(values.reverse());//[0,1,5,10,15]

let numbers=[1,2,3,4,5,4,3,2,1];
console.log(numbers.indexOf(4));//3
console.log(numbers.lastIndexOf(4));//5
console.log(numbers.includes(4));//true
console.log(numbers.includes(4,7));//false

const people=[
	{
		name:"Matt",
		age:27
	},
	{
		name:"Nicholas",
		age:29
	}
]
people.find((element,index,array) =>element.age<28);//{name:"Matt",age:27}
people.findIndex((element,index,array) =>element.age<28);//0

numbers.every((item,index,array) => item>2);//false
numbers.some((item,index,array) => item>2);//true
numbers.filter((item,index,array) => item>2);//[3, 4, 5, 4, 3]
numbers.map((item,index,array) => item*2);//[2, 4, 6, 8, 10, 8, 6, 4, 2]
numbers.forEach((item,index,array) =>{
	//执行某些操作
})
numbers.reduce((prev,cur,index,array) => prev+cur);//25 (从左至右归并)
numbers.reduceRight((prev,cur,index,array) => prev+cur);//25 (从右至左归并)
  • Set
const s=new Set();
s.add("Matt");
s.add("Frisibie");
console.log(s.has("Matt"));//true
console.log(s.size);//2
s.delete("firstName");//只删除一个值
s.clear();//清除所有值
  • WeakSet(弱集合)
  • Map
const m=new Map();
m.set("firstName","Matt");
m.set("lastName","Frisibie");
console.log(m.has("firstName"));//true
console.log(m.get("firstName"));//"Matt"
console.log(m.size);//2
m.delete("firstName");//只删除一个键值对
m.clear();//清除所有键值对
  • WeakMap(弱映射)

3.迭代器和生成器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值