Javascript教程(十一)——Date对象 || Math对象 || 包装类 || 字符串常用方法

继续讲其他的内建对象

一、Date对象

在JS中使用Date对象来表示一个时间

创建一个Date对象

  • 如果直接使用构造函数创建一个Date对象,则会封装为当前代码执行的时间
var d = new Date();
// 输出当前代码执行时候的时间
console.log(d);   // Thu Dec 10 2020 21:35:25 GMT+0800 (中国标准时间)

创建一个指定的时间对象

  • 需要在构造函数中传递一个表示时间的字符串作为参数
//日期的格式  月份/日/年 时:分:秒
var d2 = new Date("2/18/2011 11:10:30");

console.log(d2);   //Fri Feb 18 2011 11:10:30 GMT+0800 (中国标准时间)

日期对象的方法

var d2 = new Date("2/18/2011 11:10:30");

1、getDate()

  • 获取当前的日期对象是几号
var date = d2.getDate();

console.log("date = ",date);   //date = 18

2、getDay()

  • 获取当前日期对象是周几
    • 会返回一个0-6的值
      • 0 表示周日
      • 1 表示周一
      • ……
var day = d2.getDay();

console.log("day=",day);  //day = 5 表示当前日期是周五

3、getMonth()

  • 获取当前时间对象的月份
    • 会返回一个0-11的值-
    • 0 表示1月
    • 1 表示2月
    • ……
    • 11 表示12月
var month = d2.getMonth();

console.log("month=",month);  //month = 1 表示当前时间是2月

4、getFullYear()

  • 获取当前日期对象的年份
var year = d2.getFullYear();

console.log("year=",year);  //year = 2011 表示当前时间是2011年

5、getTime()

  • 获取当前日期对象的时间戳
  • 时间戳
    • 指的是从格林威治标准时间的1970年1月1日,0时0分0秒
    • 到当前日期所花费的毫秒数(1秒 = 1000毫秒)
  • 计算机底层在保存时间时使用都是时间戳
var time = d2.getTime();

console.log(time);  //1297998630000
console.log(time/1000/60/60/24/365);  //41.15926655251142,算出来离1970年1月1日经过了这么多年


var d3 = new Date("1/1/1970 0:0:0");
var time2 = d3.getTime();   
console.log(time2);   //-2800000,为什么会有差别呢?因为我们传参的1/1/1970 0:0:0是我们中国的时间,所以离格林威治时间还有8小时。

我们还可以利用时间戳来测试代码的执行的性能

//获取当前的时间戳
var start = Date.now();

for(var i=0 ; i<100 ; i++){
    
    
	console.log(i);
}

var end = Date.now();

console.log("执行了:"+(end - start)+"毫秒");   //执行了:2毫秒

二、Math对象

  • Math和其他的对象不同,它不是一个构造函数
  • 它属于一个工具类不用创建对象(也就是不用new),它里边封装了数学运算相关的属性和方法

Math的属性和方法

  • Math.PI——表示的圆周率
  • Math.abs(num)——计算数num的绝对值
  • Math.ceil()——对一个数进行向上取整小数位只要有值就自动进1
  • Math.floor()——对一个数进行向下取整小数部分会被舍掉
  • Math.round() ——对一个数进行四舍五入取整
console.log(Math.PI);    //3.1415926535……

console.log(Math.abs(-1));  // 1

console.log(Math.ceil(1.1));   //2
console.log(Math.floor(1.99));  //2
console.log(Math.round(1.4));  //1
  1. Math.random()
    • 可以用来生成一个0-1之间的随机数
  2. Math.random()*x
    • 生成一个0~x的随机数(不包括0和x)
  3. Math.round(Math.random()*x) —— round四舍五入掉了小数,只留整数,所以0会出现,x不会
    • 生成一个0~x之间的随机整数
  4. Math.round(Math.random()*(y-x)+x)
    • 生成一个x-y之间的随机整数
for(var i=0 ; i<10 ; i++){
    
    
	//console.log(Math.round(Math.random()*10));
	//console.log(Math.round(Math.random()*20));
	
	//console.log(Math.round(Math.random()*9)+1);
	//console.log(Math.round(Math.random()*8)+2);
	
	//生成1-6之间的随机数
	console.log(Math.round(Math.random()*5+1));
}
  • Math.max(num1,num2,…,numn) —— 获取多个数中的最大值
  • Math.min(num1,num2,…,numn) —— 获取多个数中的最小值
  • Math.pow(x,y) —— 返回x的y次幂
  • Math.sqrt(num) —— 用于对一个数进行开方运算

三、包装类

我们知道,数据类型分两大类:

  1. 基本数据类型
    • String Number Boolean Null Undefined
  2. 引用数据类型
    • Object

而在JS中为我们提供了三个包装类,通过这三个包装类可以将基本数据类型的数据转换为对象:

  1. String()
    • 将基本数据类型字符串转换为String对象
  2. Number()
    • 将基本数据类型的数字转换为Number对象
  3. Boolean()
    • 将基本数据类型的布尔值转换为Boolean对象
//创建一个Number类型的对象
//num = 3;
var num = new Number(3);
var str = new String("hello");
var bool = new Boolean(true);

console.log(typeof num);  //object
console.log(typeof str);  //object
console.log(typeof bool);  //object

但是注意:
我们在实际应用中不会使用基本数据类型的对象,如果使用基本数据类型的对象,在做一些比较时可能会带来一些不可预期的结果。比如:

var num = new Number(3);
var num2 = new Number(3);

console.log(num==num2);   //false

方法和属性只能添加给对象,不能添加给基本数据类型

  • 当我们对一些基本数据类型的值去调用属性和方法时,
  • 浏览器会临时使用包装类将其转换为对象,然后在调用对象的属性和方法
  • 调用完以后,在将其转换为基本数据类型
var s = 123;
			
s = s.toString();  //看起来是调用的s的方法,其实s已经被包装成一个number对象了,所以其实是调用的这个number对象的toString()方法

s.hello = "你好";   //同理这也是向这个被包装成number对象的s添加属性hello
// 但随即就又转回了基本数据类型
			
console.log(s.hello);  //结果是undefined
//为什么呢?明明给s添加了这个hello属性了呀。
// 刚刚说到,s再调用完hello之后又变回了基本数据类型,而此时的console.log中的s.hello,实际也是将s又包装了一次,变成了number对象(但已经和上次包装的s对象不是同一个了)后给了一个hello的属性,但是并没有赋值,所以是undefined

四、字符串方法

在底层,字符串其实是以字符数组的形式保存的,比如:

// 创建一个字符串
var str = "Hello Atguigu";
/*
 *
 * 它其实是这样保存的:["H","e","l","l",'o",......]
 */
1、length属性
  • 用来获取字符串的长度
console.log(str.length);  //13
console.log(str[0]);    //"H"
2、charAt()方法
  • 返回字符串中指定位置的字符
  • 根据索引获取指定的字符
str = "Hello Atguigu";
var result = str.charAt(6);

console.log(result);  //"A"
3、charCodeAt()方法
  • 获取指定位置字符的字符编码(Unicode编码)
  • 根据索引获取指定的字符
str = "Hello Atguigu";
result = str.charCodeAt(0);
console.log(result);  //72,  是“H”的Unicode编码

str = "中Hello Atguigu";
result = str.charCodeAt(0);
console.log(result);  //20013  是“中”的Unicode编码
4、String.formCharCode()方法
  • 根据字符编码去获取字符
result = String.fromCharCode(72);
console.log(result);  //“H”

result = String.fromCharCode(20013);
console.log(result);  //“中”
5、 concat()方法
  • 用来连接两个或多个字符串
  • 作用和+一样
str = "Hello Atguigu";
result = str.concat("你好","再见");

console.log(result);  //Hello Atguigu你好再见
6、 indexof()方法
  • 该方法可以检索一个字符串中是否含有指定内容
    • 如果字符串中含有该内容,则会返回其第一次出现的索引
    • 如果没有找到指定的内容,则返回-1
  • 可以指定一个第二个参数,指定开始查找的位置的索引
str = "hello hatguigu";	
		
result = str.indexOf("h",1);
console.log(result);  //6

result = str.indexOf("h",7);
console.log(result);  //-1
7、 lastIndexOf()方法
  • 该方法的用法和indexOf()一样,
    • 不同的是indexOf是从前往后找,
    • 而lastIndexOf是从后往前
  • 也可以指定开始查找的位置
str = "hello hatguigu";	
		
result = str.lastIndexOf("h");
console.log(result);  //6,从后往前,先找到6这个位置的h
8、 slice()方法
  • 从字符串中截取指定的内容
    • 不会影响原字符串,而是将截取到内容返回
    • 参数:
      • 第一个,开始位置的索引(包括开始位置)
      • 第二个,结束位置的索引(不包括结束位置)
        • 如果省略第二个参数,则会截取到后边所有的
        • 也可以传递一个负数作为参数,负数的话将会从后边计算
str = "abcdefghijk";
			
result = str.slice(1,4);
console.log(result);  //“bcd”

result = str.slice(1,-1);
console.log(result);  //“bcdefghij”
9、 substring()方法
  • 用来截取一个字符串,与slice()类似
  • 参数:
    • 第一个:开始截取位置的索引(包括开始位置)
    • 第二个:结束位置的索引(不包括结束位置)
  • 不同的是这个方法不能接受负值作为参数,如果传递了一个负值,则默认使用0
  • 而且他还自动调整参数的位置,如果第二个参数小于第一个,则自动交换
str = "abcdefghijk";
			
result = str.substring(0,2);
console.log(result);  //“ab”

result = str.substring(1,-1);  //相当于是substring(1,0) 而且还会交换成(0,1)
console.log(result);  //“a”
10、 substr()方法
  • 用来截取一个字符串
  • 参数:
    • 第一个:截取开始位置的索引
    • 第二个:截取的长度
str = "abcdefghijk";
			
result = str.substr(3,2);
console.log(result);  //“de”
11、 split()方法
  • 将一个字符串拆分为一个数组
  • 参数:
    • 需要一个字符串作为参数,将会根据该字符串去拆分数组
str = "abc,bcd,efg,hij";

result = str.split(",");		
console.log(result);  //"abc,bcd,efg,hij"result就成为一个数组了
console.log(Array.isArray(result));  //true
console.log(result[0]);  //"abc"
  • 如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素
str = "abcbcdefghij";

result = str.split("");
console.log(result);  //"a,b,c,b,c,d,e,f,g,h,i,j"
console.log(result[0]);  //"a"
12、 toUpperCase()和toLowerCase()和方法

toUpperCase():

  • 将一个字符串转换为大写并返回
    toLowerCase():
  • 将一个字符串转换为小写并返回
str = "abcdefg";
			
result = str.toUpperCase();
console.log(result); //ABCDEFG
===========================================
str = "ABCDEFG";

result = str.toLowerCase();
console.log(result);  //abcdefg

猜你喜欢

转载自blog.csdn.net/qq_32755875/article/details/111002674
今日推荐