JavaScript内置对象及内置函数学习

基本包装类型

通过这三个包装类可以将基本数据类型的数据转换为对象
Boolean,Number,不建议直接使用这两种包装器类型

Boolean

将基本数据类型的布尔值转换为Boolean对象

Number

将基本数据类型的数字转换为Number对象

var num1=3;
var num2=new Number(3);
console.log(typeof num1,typeof num2);//number object

String

将基本数据类型字符串转换为String对象

var str="hello";
//在底层字符串是以字符数组的形式保存的["h","e","l","l","o"]
方法描述
length属性,获取字符串的字符数量
charAt(i)返回给定位置的字符
charCodeAt(i)返回给定位置字符的字符编码
indexOf (“x”)从前往后查找指定字符所在位置,如果字符存在,则返回字符索引位置,如果字符不存在,则返回-1。有一个数字类型的可选参数,代表从哪个位置开始查找
lastIndexOf(“x”)从后往前查找字符所在位置
var str="hello";
console.log(str.length);//5
console.log(str.charAt(1));//e 和str[1]效果一样
console.log(str.charCodeAt(1));//101
console.log(str.indexOf("e"));//1
console.log(str.indexOf("e",2));//-1 从索引2的位置开始查找e
console.log(str.lastIndexOf("e"));//1
方法描述
concat()将一个或多个字符串进行拼接,调用者:str2,参数:str2,返回值:str1+str2组成的新字符串。
var str="hello";
var str2=",world";
console.log(str.concat(str2));//hello,world
//大部分情况使用+替代该方法
方法描述
slice[start,end)截取指定字符串,不改变原始字符串,注意使用新变量来接收返回结果
var str="helloworld";
console.log(str);//helloworld
var str2=str.slice(3,7);//新变量来接收截取的值
console.log(str2);//lowo
console.log(str);//helloworld 原数组未改变
var str3=str.slice(2);//lloworld 如果省略第二个参数,会截取到该位置后面所有的
方法描述
substr(开始位置from,截取长度length)截取字符串
substring[开始位置start,结束位置end)截取字符串
var str="helloworld";
console.log(str.substr(3,7));//loworld 从3截取,长度为7的字符串
var str="helloworld";
console.log(str.substring(3,7));//loworld 从3截取到7
方法描述
trim();删除字符串前后空格,返回被处理过的字符串
var str=" hello world ";
console.log(str);// hello world
console.log(str.trim());//hello world 字符串前后空格被删除了,中间的空格仍存在
方法描述
toLowerCase转换为小写
toUpperCase转换为大写
var str="hello world";
console.log(str.toUpperCase());//HELLO WORLD 全部转为大写
console.log(str);//hello world 不改变原字符串

Math对象

比较方法

方法描述
Math.min()返回一组数中的最小值
Math.max()返回一组数中的最大值
console.log(Math.min(10,9,5,4,2,1,3));//1
console.log(Math.max(10,9,5,4,2,1,3));//10

小数舍入为整数的方法

方法描述
Math.ceil()向上舍入
Math.floor()向下舍入
Math.round()四舍五入
console.log(Math.ceil(10.41));//11

console.log(Math.floor(10.41));//10
console.log(Math.floor(10.91));//10

console.log(Math.round(10.41));//10
console.log(Math.round(10.51));//11

随机数

方法描述
Math.random()返回0—1之间的一个随机数
console.log(Math.random());//0.46264706238334696

其他方法

方法描述
Math.abs(num)返回绝对值
Math.exp(num)返回Math.E的num次幂
Math.log(num)返回num的自然对数
Math.pow(mum,power)返回num的power次幂
Math.sqrt(num)返回num的平方根
…………

这些方法在使用时查询MDN即可

console.log(Math.abs(-3));//3
console.log(Math.sqrt(4));//2

Date对象

新建

var date=new Date();
console.log(date);//2021-09-02T02:26:49.773Z 获取当前标准时间
//如果直接使用构造函数创建一个Date对象,则会封装当前代码执行的时间

方法

方法描述
getFullYear()年份
getMonth()月份 0-11 一月-十二月
getDate()返回当前日期对象中的几号
getDay()返回星期几 周日返回0,周一-周六返回1-6
getYear()Date对象中的年份值-1900
var str="2021-09-02";
var date=new Date(str);
console.log(date.getDate());//2
console.log(date.getDay());//4
console.log(date.getFullYear());//2021  截取年份
console.log(date.getMonth()+1);//9
console.log(date.getYear());//121
方法描述
getHours()返回小时,几点了 0-23
getMinutes返回分钟数 0-59
getSeconds()返回秒钟数 0-59
getMilliseconds()毫秒
getTime()日期对象以毫秒形式返回 1970年1月1日午夜到现在的毫秒数
valueOf()将Date对象转为毫秒形式返回
toDateString()以字符串形式返回一个Date的日期部分
toTimeString()以字符串形式返回一个Date的时间部分
toISOString()将一个Date对象转换为ISO-8601格式的字符串,返回的字符串格式为yyyy-mm-ddThh:mm:ssZ
toJSON()JSON序列化一个对象
toString()将一个Date转换为一个字符串
console.log(date);//2021-09-02T03:05:32.767Z
console.log(date.getHours());//11
console.log(date.getMinutes());//5
console.log(date.getSeconds());//32
console.log(date.getMilliseconds());//495
console.log(date.getTime());//1630552243579
console.log(date.valueOf());//1630552559650
console.log(date.toDateString());//Thu Sep 02 2021
console.log(date.toTimeString());//11:18:53 GMT+0800 (GMT+08:00)
console.log(date.toISOString());//2021-09-02T03:20:12.428Z
console.log(date.toJSON());//2021-09-02T03:20:12.428Z
console.log(date.toString());//Thu Sep 02 2021 11:21:27 GMT+0800 (GMT+08:00)

例题

输出 2021-09-02 11:24:33 类型的时间

重新封装

dateFMT(new Date())
function dateFMT(date){
    var date=new Date();
    var y = date.getFullYear();
    var m = date.getMonth()+1<10?"0"+(date.getMonth()+1):date.getMonth()+1;
    // 判断月份时,采用三目运算符?: 当月份小于10时,就采用"0"+(date.getMonth()+1),在前面加0,大于10时就正常输出
    var d=date.getDate().toString().padStart(2,"0");

    var hh = date.getHours();
    var mm= date.getMinutes()+1<10?"0"+(date.getMinutes()+1):date.getMinutes()+1;
    var ss = date.getSeconds();
    return y +"-"+m +"-"+d +" "+hh +":"+mm +":"+ss ;
}
console.log(dateFMT()); //2021-09-02 16:05:59

第三方库moment

在这里插入图片描述

var moment=require("moment");
var date=new Date();
var result=moment(date).format("YYYY-MM-DD hh:mm:ss");
console.log(result);//2021-09-02 04:04:59

时间戳转换

var date=new Date();
console.log(date.getTime());//1630569899456
var Time=date.getTime();
var time=moment(Time).format("YYYY-MM-DD hh:mm:ss");//将时间戳转换为YYYY-MM-DD hh:mm:ss格式的时间
console.log(time);//2021-09-02 04:04:59
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值