JavaScript-内置对象-笔记

1.Math对象
Math对象: 用于进行数字处理
自带的对象 不是构造函数---不需要new
属性: Math.PI ----- 圆周率
方法:
Math.floor(数字) ----- 向下取整 (只要整数位)
Math.ceil(数字) ---- 向上取整(不管小数位是几,直接进一)
Math.round(数字) ---- 四舍五入取整
Math.abs(数字) ---- 绝对值
Math.max(数字1,数字2...) ---- 求最大值
Math.min(数字1,数字2...) ---- 求最小值
Math.random() ----- 生成0-1之间的随机数 0可以取到 1不行
生成 a-b之间的随机整数 包含ab 的公式 Math.round(Math.random() * (b-a)) + a


console.log(Math.PI); // 3.141592653589793
// 假设圆的半径是 2 输出周长: 2*半径*Π
console.log( 2*2*Math.PI );
console.log( Math.floor(3.94) ); // 3
console.log( Math.ceil(3.14) ); // 4
console.log( Math.round(3.14) ); // 3
console.log( Math.round(3.94) ); // 4
// 绝对值: 4----4 -5 ----- 5
console.log( Math.abs(4) );
console.log( Math.abs(-5) );
// 最大值
console.log( Math.max(20,5,78,14) ); // 78
var arr1 = [20,5,78,14]
console.log( Math.max(...arr1) ); // ... 可以将数组中的数字都拿出来 max()只能放数字
console.log( Math.min(3,5) ); // 3
console.log( Math.min( ...arr1 ) ); // 5
console.log( Math.random() ); // [0,1) 0-1之间 可以取到0 不能取到1
// 生成 0-5之间的随机整数
console.log( Math.round( Math.random()*5 ) ); // [0,5]
// 生成 2-7之间的随机整数 包含2 包含7 0-5 + 2 = 2-7
console.log( Math.round(Math.random() * 5) + 2);
// 生成 a-b之间的随机整数 包含ab 公式 Math.round(Math.random() * (b-a) ) + a
 // 生成 3-16之间的随机整数
console.log( Math.round( Math.random()*(16-3) + 3) );


2.Date对象
Date 对象 : 用于处理时间和日期
是构造函数 必须先 new 再使用
var 变量 = new Date() ------- 获取的是此时此刻的日期和时间
var 变量 = new Date('年/月/日 时:分:秒')
年月日之间可以使用 / , -
年月日 ----- 年月日 时分秒 ----- 年月日 时分
日期对象的方法:
获取年 : 日期对象.getFullYear() ---- 输出4位的年
获取月 : 日期对象.getMonth() ---- 0-11 需要加一
获取日 : 日期对象.getDate()
获取周几 : 日期对象.getDay() 1-7之间的数字
获取小时 : 日期对象.getHours() 0-60
获取分钟 : 日期对象.getMinutes() 0-60
获取秒 : 日期对象.getSeconds() 0-60

3.日期对象案例--补零操作
// 在页面中输出时间 格式是 2023年09月08日 08:08:08
// 获取日期对象
 var a = new Date('2023/9/8 8:8:8')
console.log(a); // 显示的时候时完整的日期信息 并且是8 不是08
// 单独获取 年月日 时分秒
var year = a.getFullYear()
var month = a.getMonth()+1
var date = a.getDate()
var hh = a.getHours()
var mm = a.getMinutes()
var ss = a.getSeconds()
// 进行补零操作 重新赋值: 如果小于10,前边拼接上0  否则还是自己
month = month<10 ? '0'+month : month
date = date<10 ? '0'+date : date
hh = hh<10 ? '0'+hh : hh
mm = mm<10 ? '0'+mm : mm
ss = ss<10 ? '0'+ss : ss
document.write(`${year}年${month}月${date}日 ${hh}:${mm}:${ss} `)


4.时间戳
时间戳:距离1970年1月1日 的毫秒数 1秒 = 1000毫秒
获取时间戳几种办法:
日期对象.valueOf()
日期对象.getTime()
+日期对象

5.数组对象的删除和排序
1)数组的删除 
数组.splice(下标,个数) : 从指定下标开始,删除几个元素
var arr = [67, 4, 56, 3, 7]
// 删除下标为2 的元素
arr.splice(2, 1)
console.log(arr);

2)数组的排序
数组.sort(function(a,b){
            return a-b
})
return a-b 从小到大
return b-a 从大到小
var arr1 = [67, 4, 4, 56, 4, 3, 4, 7]
arr1.sort(function(a,b){
         return a-b
})
console.log(arr1);


6.String 对象
创建字符串的三种方式
// 1、字面量方式
var str1 = 'hello'
console.log( typeof str1); // string 使用字面量创建 ----简单的字符类型
// 2、 函数方式 String() ----- 构造函数(函数名首字母大写)
var str2 = String("hello")
console.log( typeof str2); // string ----简单的字符类型
// 3、使用new 关键字
var str3 = new String('hello')
console.log(typeof str3); // object ----- 是对象
// 注意: 不管哪种方式创建的字符串,可以直接当作对象使用 可以调用方法 访问属性
// 字符串的长度 字符串.length 是属性


1)根据字符返回下标
根据字符返回位置(下标)
字符串.indexOf('指定字符',开始位置):从前往后 返回第一次出现的下标,如果没有就是-1
字符串.lastIndexOf('指定字符',开始位置): 从后往前找 返回第一次出现的下标,如果没有就是-1
var str1 = '你好世界,你好世界'
console.log( str1.indexOf('你') ); // 1  第一个'你好世界'中的'你'的下标
console.log( str1.indexOf('欢') ); // -1
console.log( str1.indexOf('世',4) ); // 7
console.log( str1.lastIndexOf('世') ); // 7  第二个'你好世界'中的'世'的下标
2)根据下标返回字符
根据下标返回字符
字符串.charAt(下标):返回对应的字符
字符串.charCodeAt(下标): 返回的是字符对应的的ASCII码 0-65535
字符串[下标] : 返回对应的字符,但是有兼容性问题
var str1 = '你好世界,你好世界'
console.log( str1.charAt(0) ); // 你
console.log( str1.charCodeAt(0) );
console.log( 'abc'.charCodeAt(0) ); // 97
console.log( str1[3] ); // 界
3)其他方法
// 1、 字符串.concat(多个字符串) 将多个字符串拼接一起
// 2、 字符串.substr(开始下标,长度): 截取部分内容
// 3、字符串.slice(开始下标,结束下标): 截取部分内容,取不到结束下标
// 4、字符串.split('指定字符'):根据指定字符进行分割,得到的是数组
// 5、字符串.replace('旧字符','新字符'): 使用7旧字符替换新字符 ----- 只替换一次
// 6、字符串.replaceAll('旧字符','新字符'): 使用旧字符替换新字符 ----- 替换所有
// 字符串的所有方法都是得到一个结果 ---- 可以输出 可以使用变量接收

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

编程在手天下我有

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

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

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

打赏作者

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

抵扣说明:

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

余额充值