网站首页 > 教程文章 正文
js里面的对象分成三大类:
- 内置对象
- Array Date Math
- 宿主对象
- 浏览器提供的对象(如bom、dom等等)
- 自定义对象
- 开发人员自己定义的对象
内置对象 ——
所谓内置对象,就是JavaScript自带的一些功能对象,里面提供了很多常用、实用的属性和方法,我们需要做的就是学习这些属性和方法怎么使用,在需要用的时候直接用就行
1.1、Math
数学对象 - 用于进行一些和数学相关的运算(计算)的
1、Math.random() 生成一个 [0,1) 之间的随机浮点数
var r = Math.random();
console.log(r);// 每次执行等到的数字都一样
2、Math.floor(x) 浮点数进行向下取整
console.log(Math.floor(3.1)); // 3
console.log(Math.floor(3.9)); // 3
console.log(Math.floor(-3.1)); // -4
console.log(Math.floor(-3.9)); // -4
向下取整就是从这个数字开始,朝数轴的左边找到一个离它最近的整数
3、Math.round(x) 浮点数四舍五入取整
console.log(Math.floor(3.1)); // 3
console.log(Math.floor(3.9)); // 4
console.log(Math.floor(-3.1)); // -3
console.log(Math.floor(-3.9)); // -4
4、Math.ceil(x) 浮点数进行向上取整
console.log(Math.floor(3.1)); // 4
console.log(Math.floor(3.9)); // 4
console.log(Math.floor(-3.1)); // -3
console.log(Math.floor(-3.9)); // -3
向上取整就是从这个数字开始,朝数轴的左边找到一个离它最近的整数
5、Math.abs(x) 求一个数的绝对值
console.log(Math.abs(3)); // 3
console.log(Math.abs(-3)); // 3
6、Math.max(x,y...) 求多个数字中的最大值
console.log(Math.max(10,20)); // 20
console.log(Math.max(8,4,5,7,3)); // 8
这个方法的参数个数是不限定的,想写几个就写几个
7、Math.min(x,y...) 求多个数字中的最小值
console.log(Math.max(10,20)); // 10
console.log(Math.max(8,4,5,7,3)); // 3
这个方法的参数个数是不限定的,想写几个就写几个
练习:
求[10,100]之间的随机整数
Math.floor(Math.random()*(max-min+1)+min)
1.2、Array对象
1、push()从数组最后增加成员
var aList = [1,2,3,4];
var res = aList.push(5);
console.log(res); //5 返回添加元素后,数组的长度
console.log(aList); //[1,2,3,4,5] 原数组。改变
2、pop()从数组最后删除成员
var aList = [1,2,3,4];
var res = aList.pop();
console.log(res); //4 返回被删除的元素
console.log(aList); //[1,2,3] 原数组。改变
3、unshift()从数组前面增加成员
var aList = [1,2,3,4];
var res = aList.unshift(5);
console.log(res); //5 返回添加元素后,数组的长度
console.log(aList); //[5,1,2,3,4] 原数组。改变
4、shift()从数组前面删除成员
var aList = [1,2,3,4];
var res = aList.shift();
console.log(res); //1 返回删除的元素
console.log(aList); //[2,3,4] 原数组。改变
5、reverse() 将数组反转
var aList = [1,2,3,4];
var res = aList.reverse();
console.log(res); //[4,3,2,1] 返回翻转后的数组
console.log(aList); //[4,3,2,1] 原数组。改变
6、splice(index,多少,项1,项2...) : 返回删除的项目删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个项,来替换那些被删除的元素(如果没有,则不替换)
var aList = [1, 2, 3, 4];
var res = aList.splice(2, 1,'a','b');
console.log(res); //[3] 返回切割掉的元素
console.log(aList); //[1,2,'a','b',4] 原数组。改变
以下的方法不会改变原数组
7、slice(start,end): 返回切割项目选取从数组 start位置 到数组的 end位置(不包含end位置) 所有元素,如果没有end,选取从 start 到数组结尾的所有元素
var aList = [1,2,3,4];
var res = aList.slice(0,2);
console.log(res); //[1,2] 返回切割掉的元素
console.log(aList); //[1,2,3,4] 原数组。不变
8、indexOf() 返回数组中元素第一次出现的索引值
var aList = [3,2,"a",1,2,3,"a"];
var res = aList.indexOf("a");
console.log(res); //2
var res2 = aList.indexOf("a",3);
console.log(res2)
9、join() 将数组成员通过一个分隔符合并成字符串
var aList = [1,2,3,4];
var res = aList.join('-');
console.log(res); //1-2-3-4 返回合并后的字符串
console.log(aList); //[1,2,3,4] 原数组。不变
10、forEach 遍历数据的方法
// 作用:遍历数组
arr.forEach(function(item,index){
item 是数组里面的每个元素
index 是数组的每个索引
})
11、filter 数组筛选之后返回新数组
// 作用:把数组中满足条件的元素,放到一个新的数组里面
var result = arr.filter(function(item,index){
return 条件
// item就是当前遍历到的这个元素, index是这个元素索引
return item > 20
});
12、map 数据处理之后返回新数组
var newArr = arr.map(function(item,index){
item+=3;
return item;
})
1.3、String内置对象
1、字符串合并操作:“ + ”
var num1 = 123;
var str2 = 'def';
console.log(str1+str2) // 123def
2、split() 把一个字符串按某种方式分隔成字符串组成的数组
var str2 = 'a-b-c';
var arr2 = str4.split('-');
console.log(arr4) // ['a','b','c']
3、charAt() 获取字符串中的某一个字符
var str3 = 'hello word'
var res = str3.charAt(2)
console.log(res)//l
4、indexOf() 查找字符串是否含有某字符(和数组一样的使用方式)
5、substring() 截取字符串 用法:substring(start,end)(不包括end)截取从star开始,到end之间的字符串
如果只传一个值,表示从这个位置开始,一致截取到字符串末端
注意: substring是以两个参数中较小一个作为起始位置,较大的参数作为结束位置
var test = 'hello world';
alert(test.substring(7,4)); //o w
注意: 如果substring里面碰到负数,则干脆将负参数都直接转换为0
var test = 'hello world'
alert(test.substring(-3)); //hello world
alert(test.slice(3,-4)); //lo w
6、substr(index,n) : 从index索引位置开始截取,截取n个字符
如果只传一个值,表示从这个位置开始,一致截取到字符串末端
7、toUpperCase() 字符串转大写8、toLowerCase() 字符串转小写9、replace(旧字符, 新字符) 该方法会返回一个新字符串,原字符串不改变
var str9 = 'hello word'
var res = str.replace('word','hhhhhhhh')
console.log(res)// hello hhhhhhhh
1.4、Date内置对象
var date = new Date();
// 获取年份
var year = date.getFullYear();
console.log(year);
// 获取月份 , 得到的月份是从0开始的 ,使用 0-11 表示 1-12 月
var month = date.getMonth()+1;
console.log(month);
// 获取天 几号
var day = date.getDate();
console.log(day);
// 获取星期数
var d = date.getDay();
// 获取小时
var h = date.getHours();
console.log(h);
// 获取分钟
var m = date.getMinutes();
console.log(m);
// 获取秒数
var s = date.getSeconds();
console.log(s);
// 获取毫秒
var ms = date.getMilliseconds();
// 返回事件戳。1970年1月1日 0时0分0秒到现在的毫秒值
// 最初计算机操作系统是 32 位,而时间也是用 32 位表示。
// 最长时间是 68 年
// 最早出现的 UNIX 操作系统考虑到计算机产生的年代和应用的时限
// 综合取了 1970 年 1 月 1 日作为 UNIX TIME 的纪元时间
var dateTime = date.getTime();
// 把需要补0的先补上
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
if (h < 10) {
h = '0' + h;
}
if (m < 10) {
m = '0' + m
}
if (s < 10) {
s = '0' + s;
}
console.log("现在是: " + year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s + ",星期" + d);
也可以生成一个指定的日期
1.传入不同的年月日时分秒
var d1 = new Date(2020,0,1,8,30,59); // 2020年1月1日 8:30:59
2.传入一个指定的字符串
var d2 = new Date('2020-01-01');
3.传入一个 大的数字 -- 从1970年1月1日开始到某个日期的总共的毫秒数
var d3 = new Date(1378594320999);
- 上一篇: JavaScript 数组操作方法大全
- 下一篇: 不产生新的数组,删除数组里的重复元素
猜你喜欢
- 2025-05-23 JavaScript巩固基础每日随记之[数组]
- 2025-05-23 你应该掌握的 10 种 JavaScript 对象处理技巧
- 2025-05-23 更简单的Vue3中后台动态路由 + 侧边栏渲染方案
- 2025-05-23 2023:Js中新增四个不修改原数组的方法
- 2025-05-23 常见vue面试题,大厂小厂都一样
- 2025-05-23 在vue实现element ui中的card(卡片中)使用多选和分页
- 2025-05-23 js数组常用方法总结
- 2025-05-23 php手把手教你做网站(三十八)jquery 转轮盘抽奖,开盲盒
- 2025-05-23 关于数组的操作方法
- 2025-05-23 javascript基础入门
- 05-25干货 | 一步步部署 Flask 应用
- 05-25别再去找Docker命令了,你要的常用的全都在这
- 05-25如果您删除Windows11上的“Program Files”文件夹会发生什么?
- 05-25家用nas最常用的docker容器及部署方法
- 05-25你好 dotnet run file, 再见 csproj
- 05-25China committed to continuing contributions to global health: delegation
- 05-25Chinese, German experts urge cooperation during Eurasia relations seminar
- 05-25Peace of paramount importance for region
- 最近发表
-
- 干货 | 一步步部署 Flask 应用
- 别再去找Docker命令了,你要的常用的全都在这
- 如果您删除Windows11上的“Program Files”文件夹会发生什么?
- 家用nas最常用的docker容器及部署方法
- 你好 dotnet run file, 再见 csproj
- China committed to continuing contributions to global health: delegation
- Chinese, German experts urge cooperation during Eurasia relations seminar
- Peace of paramount importance for region
- after和in用法解析
- China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)