各种对象详解【object】
A.ARRAY数组对象
1.数组遍历的Api
forEach 遍历数值的api
forEach 里面的参数是一个回调函数(回头再调用)
回调函数中的
参数1 表示的是当前数组中的元素
参数2 是该元素对应的索引
参数3是该元素所在的数组
复制代码
var arr = [2,3,45,67,8,9,10];
arr.forEach(function(item,index,arr){
console.log(item,index,arr);
});
复制代码
【遍历后,它会输出:元素+下标索引+所属数组】
every 去遍历数组 参数 是一个 函数
函数中的
参数1是当前遍历的元素
参数2当前元素的索引
参数3 当前元素所在的数组
复制代码
var res = arr.every(function(item){
// return 后面就是我们要检测的条件
// 只要有一个 item不满足 就返回 false
return item >100;
});
console.log(res);
复制代码
【带判断条件的遍历,判断条件是:boolean类型true&&false】
【小模块总结】
foreach(function(item,index,arr){ //遍历
})
// 所有的元素都满足条件 返回true 否则false
every(function(item,index,arr){
return item>10
})
// 只要有一个满足条件 返回 true 否则false
some(function(item,index,arr){
return item>10
})
// 处理数组中的每一个元素 返回的是一个新数组
map(function(item,index,arr){
return item+10;
})
// 过滤出符合条件的数组中的元素 并组成一个新数组 并返回 不影响原数组
filter(unction(item,index,arr){
return item>10;
})
复制代码
2.数组增删改查Api
var arr = [];
复制代码
添加数组元素的api push
向数组的末尾新增一个元素并返回新数组的长度
var res = arr.push(1, 2);
console.log(res, arr);
复制代码
添加数组元素的api unshift
向数组的开头添加一个或更多元素,并返回新的长度。
var res1 = arr.unshift(3, 4, 5);
console.log(res1, arr);
复制代码
删除数组元素的api pop
shift
从数组末尾删除元素 并返回被删除的元素
var res2 = arr.pop();
console.log(res2, arr);
复制代码
从数组开头删除元素
var res3 = arr.shift();
console.log(res3, arr);
复制代码
【小模块总结】
push
和pop
是一对栈操作 unshift
和shift
是一对队列操作
splice 向数组中添加或删除元素
参数1数组开始删除元素的位置
参数2 删除元素的个数
参数3以后 表示在参数1的位置插入新的元素
复制代码
只有参数1 表示从这个位置一直到数组最后
两个参数 表示删除的位置和个数
var arr4 = [3, 4, 5, 4, 67, 23, 4, 4, 4, 44, 4, 4];
var res4 = arr4.splice(1, 2, 999, 888);
console.log(res4, arr4);
复制代码
判断数组中是否包含某个元素 返回一个boolean值
var arr5 = [1, 22, 3, 3, 3, 3, 345, 6];
var res5 = arr5.includes(222)
console.log(res5);
复制代码
数组反转 原数组也会直接反转
var res6 = arr5.reverse();
console.log(res6, arr5);
复制代码
数组排序
var arr6 = [3, 4, 5, 67, 8732, 2];
arr6.sort(function (a, b) {
// return a-b;升序排列
return b - a; //降序
});
console.log(arr6);
复制代码
indexOf 查找元素的索引
参数1要找的元素
参数2 开始查找的索引位置 如果找不到 返回值是-1
参数2可选 没有的话 从0 开始到最后
复制代码
var arr8 = [3, 4, 5, 6, 6, 7, 7];
console.log(arr8.indexOf(6));
复制代码
slice 选取数组的一部分
参数1 开始位置 参数2 结束位置(不包括) 可选
不写就是从开始位置到结束位置
复制代码
console.log(arr8.slice(0));
复制代码
3.其他数组Api
数组合并
var arr1 = [9, 8, 7];
var arr2 = [4, 5, 6];
var res1 = arr1.concat(arr2);
console.log(res1, arr1, arr2);
复制代码
数组中的元素拷贝
数组中的元素拷贝到指定位置
参数1是指定的索引位置
参数2 开始拷贝的位置
参数3 是拷贝结束的位置
复制代码
var res2 = arr1.copyWithin(1, 0, 1);
console.log(res2);
复制代码
伪数组 : 1有索引 2有length属性
var str = "23432432432";
console.log(str[1], str.length);
复制代码
判断一个对象是否是数组
直接使用 类名调用的方法我们成为静态方法(内置的方法本来就有 相当于DNA)
var res4 = Array.isArray(str);
console.log(res4)
复制代码
from 将一个伪数组(类数组) 转变为一个真数组
var res5 = Array.from(str);
console.log(Array.isArray(res5));
复制代码
将数组按照指定的字符转为字符串
var arr6 = [1, 2, 4, 6, 677, 45];
// 参数就是字符串的分隔符 默认是 ,
console.log(arr6.join("*")) ;
复制代码
keys 返回的数组的可迭代对象 包含的是数组的键
var res = arr6.keys();
console.log(res);
for(item of res){
console.log(item);
}
复制代码
values 返回的数组的可迭代对象 包含的是数组的值
var res7 = arr6.values();
复制代码
for of 遍历
for(item of res7){
console.log(item);
}
复制代码
使用迭代器遍历 数组的键值对
var arr3 = ["H", "e", "l", "l", "o"];
console.log(arr3.entries())
复制代码
使用迭代器遍历 数组的键值对
var res = arr3.entries();
for (var item of res) {
// 打印键值对
console.log(item, item[0], item[1]);
}
复制代码
将一个数组转为字符串没有join强大
console.log(arr8.toString());
复制代码
查找数组中的元素 查找第一个符合条件的元素
var res5 = arr4.find(function (item) {
return item > 60;
})
console.log(res5)
复制代码
查找数组中符合条件的元素的索引
var res6 = arr4.findIndex(function (item) {
return item > 60
});
console.log(res6); //返回1
复制代码
数组API总结
定义数组:
var arr = [1,2,3];
var arr = new Array();
数组长度: arr.length
遍历数组 :
foreach(function(item,index,arr){})
every(function(item,index,arr){ return item>1})
返回boolean值 所有的都符合条件才会返回true
some(function(item,index,arr){ return item>1})
返回boolean 只要有一个满足条件的 返回 true
map(function(item,index,arr){ return item*10})
返回处理过的数组
filter(function(item,index,arr){ return item>10})
返回符合条件的元素 组成的新数组
for of 利用迭代器遍历数组 for(item of arr.entries()){console.log(item)} ;
增加元素: push(arg) 数组末尾新增元素 返回新数组的长度
unshift(arg) 数组开头添加元素 返回新数组的长度
splice(arg1,arg2,arg3...)
arg3以后是要插入的元素
arg1开始插入或删除的位置
arg2 表示删除的个数
删除元素: pop() 从数组末尾删除一个元素 返回被删除的元素
shift() 从数组开头删除元素 返回被删除的元素
//注意 后面的元素索引会立即发生改变
splice(arg1,arg2) arg1开始插入或删除的位置 arg2 表示删除的个数
修改元素: arr[i] = 23;
数组反转: reverse();
数组排序: sort(function(a,b){return a-b}) 升序排列
数组是否包含某个元素: includes(item); 返回 true/false
indexOf(arg1,arg2)
arg1要找的元素
arg2 开始查找的索引号
返回值是元素的索引 没有 -1
数组拼接: contact(arr);
数组拷贝: copyWithin()
数组转字符串: join("*") 可以指定分隔符 toString()
判断对象是否是数组: Array.isArray();
伪数组转为数组: Array.from(arr);
数组检索:find(function(item,index){return item>10})
找到第一个符合条件的元素
findIndex(function(item,index){return item>10})
找到第一个符合条件的元素的索引
数组截取: slice(starNum,endNum); 注意不包含endNum位置
复制代码
4.补充数组API知识
数组去重
function uniq1(arr) {
if (!Array.isArray(arr)) {
console.log("输入错误");
return;
}
var array = [];
for (var i = 0; i < arr.length; i++) {
// if (array.indexOf(arr[i]) === -1) {
// array.push(arr[i]);
// }
if (!array.includes(arr[i])) {
array.push(arr[i]);
}
}
}
复制代码
数组去重简单方法 兼容性不是太好
function uniq(arr) {
if (!Array.isArray(arr)) {
console.log("输入错误")
return;
}
return Array.from(new Set(arr));
}
console.log(uniq([1, 2, 3, 1, 1, 4, 6, 3, 1]));
复制代码
将数组转为对象 es6新增的 …
var arr = ["a", 2, 3];
var obj = { ...arr };
console.log(obj);
复制代码
合并两个对象
const obj1 = { a: 1 };
const obj2 = { b: 2 };
var obj3 = Object.assign(obj1, obj2);
console.log(obj3);
var obj4 = { ...obj1, ...obj2 };
console.log(obj4);
复制代码
深度递归
var arr1 = [1, [2, 3, [4, 5]]];
console.log(arr1.flat(3));
复制代码
求幂运算
console.log(Math.pow(2, 10));
console.log(2 ** 10);
复制代码
截断数组
var arr2 = [1, 2, 3, 4, 5];
arr2.length = 3;
console.log(arr2)
复制代码
copy数组
var arr3 = [1, 2, 3, 4, 5];
var copyarr3 = arr3.slice();
var copyarr4 = [...arr];
var copyarr5 = arr3.concat();
console.log(copyarr3);
console.log(copyarr5);
console.clear();//清空控制台
console.log(2 ** 3);
复制代码
B.Boolean 布尔对象
实例化一个Boolean对象
var bool = new Boolean(true);//默认是false
console.log(bool);
复制代码
Boolean对象也是包装对象 会自动拆箱
console.log(bool&&false);
复制代码
返回 Boolean 对象的原始值。
console.log(bool.valueOf());
复制代码
把布尔值转换为字符串,并返回结果。
console.log(bool.toString());
复制代码
C.Date 日期对象
实例化一个date对象
var date = new Date();
复制代码
获取号(天)数 getDate()
console.log(date.getDate());
复制代码
获取周几(星期) 0-6 getDay
console.log(date.getDay());
复制代码
获取年份 getFullYear()
console.log(date.getFullYear());
复制代码
获取小时 getHours()
console.log(date.getHours());
复制代码
获取分钟 getMinutes()
console.log(date.getMinutes());
复制代码
获取秒 getSeconds()
console.log(date.getSeconds());
复制代码
获取毫秒 getMilliseconds
console.log(date.getMilliseconds());
复制代码
获取月份 从0开始算 getMonth 因此需要+1
console.log(date.getMonth()+1);
复制代码
获取从 1970年 1月1号 到现在的毫秒数
console.log(date.getTime());
复制代码
UTC 格林威治时间
console.log(date.getUTCFullYear());//年
console.log(date.getUTCMonth());//月
console.log(date.getUTCDate());//日
console.log(date.getUTCDay());//周
console.log(date.getUTCHours());//时
console.log(date.getUTCMinutes());//分
console.log(date.getUTCSeconds());//秒
console.log(date.getUTCMilliseconds());//毫秒
复制代码
重置日期
date.setDate(1);
console.log(date.getDate());
复制代码
返回字符串格式的日期 周次 月份 年份
获取周 月 日 年 toDateString()
console.log(date.toDateString());
console.log(date.toJSON());
复制代码
获取年月日 toLocaleDateString()
console.log(date.toLocaleDateString());
复制代码
获取时间 toLocaleTimeString()
console.log(date.toLocaleTimeString());
复制代码
获取中国标准时间 toString()
console.log(date.toString());
复制代码
D.Math 数学对象
Math对象 封装了关于数学计算的api
/* 自然对数 Math.E */
console.log(Math.E);
/* 圆周率 π Math.PI*/
console.log(Math.PI);
/* 绝对值 Math.abs */
console.log(Math.abs(-21));
/* 立方根 Math.cbrt*/
console.log(Math.cbrt(8));
/* 向上取整 */
console.log(Math.ceil(3.14));
/* e的次方 */
console.log(Math.exp(2));
/* 向下取整 */
console.log(Math.floor(3,4));
/* 参数平方和的平方根 */
console.log(Math.hypot(3,4));
/* 计算一组数的最大值 和最小值 */
console.log(Math.max(3,4,5,6,7,8));
console.log(Math.min(3,5,4,6,7,9));
/* 幂次方 第一个参数是值 第二个参数是次方*/
console.log(Math.pow(2,3));
/* 随机数 [0,1)*/
console.log(Math.random());
/* 四舍五入 */
console.log(Math.round(4.12));
/* 平方根 */
console.log(Math.sqrt(4));
// 清空控制台
console.clear();
复制代码
Math 数学对象中的三角函数对象
/* 计算正弦 Math.sin */
document.write("<h1>正弦弧度值</h1>");
document.write("<br>我是正弦函数0度的弧度值:"+Math.sin(0*Math.PI/180));
document.write("<br>我是正弦函数30度的弧度值:"+Math.sin(30*Math.PI/180));
document.write("<br>我是正弦函数45度的弧度值:"+Math.sin(45*Math.PI/180));
document.write("<br>我是正弦函数60度的弧度值:"+Math.sin(60*Math.PI/180));
document.write("<br>我是正弦函数90度的弧度值:"+Math.sin(90*Math.PI/180));
document.write("<br>我是正弦函数120度的弧度值:"+Math.sin(120*Math.PI/180));
document.write("<br>我是正弦函数135度的弧度值:"+Math.sin(135*Math.PI/180));
document.write("<br>我是正弦函数150度的弧度值:"+Math.sin(150*Math.PI/180));
document.write("<br>我是正弦函数180度的弧度值:"+Math.sin(180*Math.PI/180));
document.write("<br>我是正弦函数270度的弧度值:"+Math.sin(270*Math.PI/180));
document.write("<br>我是正弦函数360度的弧度值:"+Math.sin(360*Math.PI/180));
/* 计算 反正弦 Math.asin */
document.write("<h1>反正弦弧度值</h1>");
document.write("<br>我是反正弦函数0度的弧度值:"+Math.asin(0*Math.PI/180));
document.write("<br>我是反正弦函数30度的弧度值:"+Math.asin(30*Math.PI/180));
document.write("<br>我是反正弦函数45度的弧度值:"+Math.asin(45*Math.PI/180));
/* 计算双曲正弦 Math.sinh*/
document.write("<h1>双曲正弦弧度值</h1>");
document.write("<br>我是双曲正弦函数0度的弧度值:"+Math.sinh(0*Math.PI/180));
document.write("<br>我是双曲正弦函数30度的弧度值:"+Math.sinh(30*Math.PI/180));
document.write("<br>我是双曲正弦函数45度的弧度值:"+Math.sinh(45*Math.PI/180));
document.write("<br>我是双曲正弦函数60度的弧度值:"+Math.sinh(60*Math.PI/180));
document.write("<br>我是双曲正弦函数90度的弧度值:"+Math.sinh(90*Math.PI/180));
document.write("<br>我是双曲正弦函数120度的弧度值:"+Math.sinh(120*Math.PI/180));
document.write("<br>我是双曲正弦函数135度的弧度值:"+Math.sinh(135*Math.PI/180));
document.write("<br>我是双曲正弦函数150度的弧度值:"+Math.sinh(150*Math.PI/180));
document.write("<br>我是双曲正弦函数180度的弧度值:"+Math.sinh(180*Math.PI/180));
document.write("<br>我是双曲正弦函数270度的弧度值:"+Math.sinh(270*Math.PI/180));
document.write("<br>我是双曲正弦函数360度的弧度值:"+Math.sinh(360*Math.PI/180));
/* 计算反双曲正弦 Math.asinh*/
document.write("<h1>反双曲正弦弧度值</h1>");
document.write("<br>我是反双曲正弦函数0度的弧度值:"+Math.asinh(0*Math.PI/180));
document.write("<br>我是反双曲正弦函数30度的弧度值:"+Math.asinh(30*Math.PI/180));
document.write("<br>我是反双曲正弦函数45度的弧度值:"+Math.asinh(45*Math.PI/180));
document.write("<br>我是反双曲正弦函数60度的弧度值:"+Math.asinh(60*Math.PI/180));
document.write("<br>我是反双曲正弦函数90度的弧度值:"+Math.asinh(90*Math.PI/180));
document.write("<br>我是反双曲正弦函数120度的弧度值:"+Math.asinh(120*Math.PI/180));
document.write("<br>我是反双曲正弦函数135度的弧度值:"+Math.asinh(135*Math.PI/180));
document.write("<br>我是反双曲正弦函数150度的弧度值:"+Math.asinh(150*Math.PI/180));
document.write("<br>我是反双曲正弦函数180度的弧度值:"+Math.asinh(180*Math.PI/180));
document.write("<br>我是反双曲正弦函数270度的弧度值:"+Math.asinh(270*Math.PI/180));
document.write("<br>我是反双曲正弦函数360度的弧度值:"+Math.asinh(360*Math.PI/180));
/* 计算余弦 Math.cos*/
document.write("<h1>余弦弧度值</h1>");
document.write("<br>我是余弦函数0度的弧度值:"+Math.cos(0*Math.PI/180));
document.write("<br>我是余弦函数30度的弧度值:"+Math.cos(30*Math.PI/180));
document.write("<br>我是余弦函数45度的弧度值:"+Math.cos(45*Math.PI/180));
document.write("<br>我是余弦函数60度的弧度值:"+Math.cos(60*Math.PI/180));
document.write("<br>我是余弦函数90度的弧度值:"+Math.cos(90*Math.PI/180));
document.write("<br>我是余弦函数120度的弧度值:"+Math.cos(120*Math.PI/180));
document.write("<br>我是余弦函数135度的弧度值:"+Math.cos(135*Math.PI/180));
document.write("<br>我是余弦函数150度的弧度值:"+Math.cos(150*Math.PI/180));
document.write("<br>我是余弦函数180度的弧度值:"+Math.cos(180*Math.PI/180));
document.write("<br>我是余弦函数270度的弧度值:"+Math.cos(270*Math.PI/180));
document.write("<br>我是余弦函数360度的弧度值:"+Math.cos(360*Math.PI/180));
/* 计算反余弦 Math.acos */
document.write("<h1>反余弦弧度值</h1>");
document.write("<br>我是反余弦函数0度的弧度值:"+Math.acos(0*Math.PI/180));
document.write("<br>我是反余弦函数30度的弧度值:"+Math.acos(30*Math.PI/180));
document.write("<br>我是反余弦函数45度的弧度值:"+Math.acos(45*Math.PI/180));
/* 计算双曲余弦 Math.cosh*/
document.write("<h1>双曲余弦弧度值</h1>");
document.write("<br>我是双曲余弦函数0度的弧度值:"+Math.cosh(0*Math.PI/180));
document.write("<br>我是双曲余弦函数30度的弧度值:"+Math.cosh(30*Math.PI/180));
document.write("<br>我是双曲余弦函数45度的弧度值:"+Math.cosh(45*Math.PI/180));
document.write("<br>我是双曲余弦函数60度的弧度值:"+Math.cosh(60*Math.PI/180));
document.write("<br>我是双曲余弦函数90度的弧度值:"+Math.cosh(90*Math.PI/180));
document.write("<br>我是双曲余弦函数120度的弧度值:"+Math.cosh(120*Math.PI/180));
document.write("<br>我是双曲余弦函数135度的弧度值:"+Math.cosh(135*Math.PI/180));
document.write("<br>我是双曲余弦函数150度的弧度值:"+Math.cosh(150*Math.PI/180));
document.write("<br>我是双曲余弦函数180度的弧度值:"+Math.cosh(180*Math.PI/180));
document.write("<br>我是双曲余弦函数270度的弧度值:"+Math.cosh(270*Math.PI/180));
document.write("<br>我是双曲余弦函数360度的弧度值:"+Math.cosh(360*Math.PI/180));
/* 计算反双曲余弦 Math.acosh*/
document.write("<h1>反双曲余弦弧度值</h1>");
document.write("<br>我是反双曲余弦函数60度的弧度值:"+Math.acosh(60*Math.PI/180));
document.write("<br>我是反双曲余弦函数90度的弧度值:"+Math.acosh(90*Math.PI/180));
document.write("<br>我是反双曲余弦函数120度的弧度值:"+Math.acosh(120*Math.PI/180));
document.write("<br>我是反双曲余弦函数135度的弧度值:"+Math.acosh(135*Math.PI/180));
document.write("<br>我是反双曲余弦函数150度的弧度值:"+Math.acosh(150*Math.PI/180));
document.write("<br>我是反双曲余弦函数180度的弧度值:"+Math.acosh(180*Math.PI/180));
document.write("<br>我是反双曲余弦函数270度的弧度值:"+Math.acosh(270*Math.PI/180));
document.write("<br>我是反双曲余弦函数360度的弧度值:"+Math.acosh(360*Math.PI/180));
/* 计算正切 Math.tan */
document.write("<h1>正切弧度值</h1>");
document.write("<br>我是正切函数0度的弧度值:"+Math.tan(0*Math.PI/180));
document.write("<br>我是正切函数30度的弧度值:"+Math.tan(30*Math.PI/180));
document.write("<br>我是正切函数45度的弧度值:"+Math.tan(45*Math.PI/180));
document.write("<br>我是正切函数60度的弧度值:"+Math.tan(60*Math.PI/180));
document.write("<br>我是正切函数90度的弧度值:"+Math.tan(90*Math.PI/180));
document.write("<br>我是正切函数120度的弧度值:"+Math.tan(120*Math.PI/180));
document.write("<br>我是正切函数135度的弧度值:"+Math.tan(135*Math.PI/180));
document.write("<br>我是正切函数150度的弧度值:"+Math.tan(150*Math.PI/180));
document.write("<br>我是正切函数180度的弧度值:"+Math.tan(180*Math.PI/180));
document.write("<br>我是正切函数270度的弧度值:"+Math.tan(270*Math.PI/180));
document.write("<br>我是正切函数360度的弧度值:"+Math.tan(360*Math.PI/180));
/* 计算反正切 Math.atan*/
document.write("<h1>反正切弧度值</h1>");
document.write("<br>我是反正切函数0度的弧度值:"+Math.atan(0*Math.PI/180));
document.write("<br>我是反正切函数30度的弧度值:"+Math.atan(30*Math.PI/180));
document.write("<br>我是反正切函数45度的弧度值:"+Math.atan(45*Math.PI/180));
document.write("<br>我是反正切函数60度的弧度值:"+Math.atan(60*Math.PI/180));
document.write("<br>我是反正切函数90度的弧度值:"+Math.atan(90*Math.PI/180));
document.write("<br>我是反正切函数120度的弧度值:"+Math.atan(120*Math.PI/180));
document.write("<br>我是反正切函数135度的弧度值:"+Math.atan(135*Math.PI/180));
document.write("<br>我是反正切函数150度的弧度值:"+Math.atan(150*Math.PI/180));
document.write("<br>我是反正切函数180度的弧度值:"+Math.atan(180*Math.PI/180));
document.write("<br>我是反正切函数270度的弧度值:"+Math.atan(270*Math.PI/180));
document.write("<br>我是反正切函数360度的弧度值:"+Math.atan(360*Math.PI/180));
/* 计算双曲正切 Math.tanh*/
document.write("<h1>双曲正切弧度值</h1>");
document.write("<br>我是双曲正切函数0度的弧度值:"+Math.tanh(0*Math.PI/180));
document.write("<br>我是双曲正切函数30度的弧度值:"+Math.tanh(30*Math.PI/180));
document.write("<br>我是双曲正切函数45度的弧度值:"+Math.tanh(45*Math.PI/180));
document.write("<br>我是双曲正切函数60度的弧度值:"+Math.tanh(60*Math.PI/180));
document.write("<br>我是双曲正切函数90度的弧度值:"+Math.tanh(90*Math.PI/180));
document.write("<br>我是双曲正切函数120度的弧度值:"+Math.tanh(120*Math.PI/180));
document.write("<br>我是双曲正切函数135度的弧度值:"+Math.tanh(135*Math.PI/180));
document.write("<br>我是双曲正切函数150度的弧度值:"+Math.tanh(150*Math.PI/180));
document.write("<br>我是双曲正切函数180度的弧度值:"+Math.tanh(180*Math.PI/180));
document.write("<br>我是双曲正切函数270度的弧度值:"+Math.tanh(270*Math.PI/180));
document.write("<br>我是双曲正切函数360度的弧度值:"+Math.tanh(360*Math.PI/180));
/* 计算双曲反正切 Math.tanh*/
document.write("<h1>双曲反正切弧度值</h1>");
document.write("<br>我是双曲反正切函数0度的弧度值:"+Math.atanh(0*Math.PI/180));
document.write("<br>我是双曲反正切函数30度的弧度值:"+Math.atanh(30*Math.PI/180));
document.write("<br>我是双曲反正切函数45度的弧度值:"+Math.atanh(45*Math.PI/180));
复制代码
E.Number 数字对象
实例化一个对象
var num1 = new Number(1);
console.log(num1);
复制代码
直接赋值创建对象
var num2 = 1;
复制代码
typeof 判断数据类型
console.log(typeof(num1),typeof(num2));//object和number
复制代码
当遇见算术运算符时 Number
类型的对象会自动拆箱 变成基本数据类型
自动拆箱(将 Number类型的对象自动转为 数字)和自动装箱(将数字 转为 Number类型的对象)
当number类型的基本数据类型 遇到调用方法时会自动装箱 变成 一个对象
console.log(num1 == num2);
console.log(num1 + 3);
复制代码
Number 对象的API
/* js中最大值 Number.MAX_VALUE */
console.log(Number.MAX_VALUE);//1.7976931348623157e+308
/* js中最小值 Number.MIN_VALUE */
console.log(Number.MIN_VALUE);//5e-324
/* 表示非数值 NaN*/
console.log(Number.NaN);
/* Number判断API 返回值是:true && false*/
var num = Infinity;//无限 Infinity
/* 判断一个数是否是有限的 isFinite*/
console.log(Number.isFinite(num));
/* 判断一个数是否是整数 isInteger*/
console.log(Number.isInteger(3));
/* 判断一个数是否是NaN isNaN*/
console.log(Number.isNaN(2));
/* 从字符串中找到数字 必须是开头就是 如果不是就返回 NaN parseFloat*/
console.log(Number.parseFloat("5555呜呜呜555555嗡嗡嗡5555哇哇哇"));
/* 将一个小数或 字符串类型的小数 变成 整数 parseInt */
console.log(Number.parseInt(66.666));
console.log(Number.parseInt("99.999"));
/* 数值转字符串toString */
var num = 99;
console.log(num.toString());
/* 保留几位小数 */
var num = 100;
console.log(num.toFixed(2));
/* 常用的Number的Api : isNaN (判断是否为非数值)
parseInt(取整)
parseFloat(寻找数字【必须第一个就是】)
toString(数字转字符串)
toFixed(保留小数) */
复制代码
F.STRING字符串对象
创建一个字符串
var str1 = "hello";
console.log(str1);
复制代码
字符串模板 `
var str =`
hello Mr.Yuan;
`;
console.log(str);
复制代码
字符串模板的拼接运算
var num =0;
var str2 = `您好!第${++num}号召唤师`;
console.log(str2);
复制代码
可以嵌套双引号和单引号 里面可以输出语句
var str3 = `${console.log("Hello!")}`;
复制代码
字符串的拼接
var title = "标题";
var imgref = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.xiazaizhijia.com%2Fwalls%2F20151203%2Fmiddle_6080601ff4a6c55.jpg&refer=http%3A%2F%2Fimg2.xiazaizhijia.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625202656&t=872589b8b9544d2a6f8f0eb1cad4e5c5"
var html = `<div>
<h1>${title}</h1>
<img src =${imgref}>
</div>`
document.write(html);
复制代码
字符串常用API
返回在指定位置的字符 charAt
var strA = "前端9523546AZCAac开始acasddxu学习c";
console.log(strA.charAt(1));
复制代码
/* 返回在指定的位置的字符的Unicode编码。charCodeAt */
// a-z 97-122 A-Z 65-90 0-9 48-57
console.log(strA.charCodeAt(0));
复制代码
将Unicode 编码转为字符 fromCharCode
console.log(String.fromCharCode(26446));
复制代码
返回某个指定的字符串值在字符串中首次出现的位置。
indexOf没有的话 返回-1
// 参数1 要查找的字符串 参数2 开始找的位置
console.log(strA.indexOf("ae",13));
console.log(strA.indexOf("a",13));
复制代码
找字符串中是否包含指定的子字符串 includes
console.log(strA.includes("端"));
复制代码
字符串的替换 replace
var strB="我认为==你是==DK
var res = strB.replace("DK,"**");
console.log(res,strB);
复制代码
截取字符串 slice
// 参数1 开始位置 是负数从后面开始截取
// 参数2(可选) 结束位置(不包括结束位置)
// 没有参数1 从开始位置一直到结束位置
var res2 = strB.slice(-3);
console.log(res2);
复制代码
把字符串分割为字符串数组。可以指定字符 split
var res3 = strB.split("=");
console.log(res3);
复制代码
字符串的联式调用 api1.api2.api3
var strC = "我想学前端";
// str.split("") 已经是一个数组了可以继续使用数组的api 称作链式调用
var res4 = strC.split("").reverse().join();//字符串转为数组 再反转 再转回字符串(链式调用)
console.log(res4);
复制代码
统计某个字母出现的个数
// 方法1
var strD = "ssfffkzlkflkkf";
function charCount(strD,a){
var count = 0;
for(var i =0;i<strD.length;i++){
if (strD[i]==a) {
count++;
}
}
return count;
}
// var res5 = charCount(strD,"f");
// console.log(res5);
// 方法2
/* 使用对象来统计字符串出现的次数 */
function tjChar(strD,a){
var obj = {};
for(var i =0;i<strD.length;i++){
// 如果对象中没该属性 返回的属性值是 undefined
if (obj[strD[i]]) {
// 如果有这个属性 该属性值就自加1
obj[strD[i]]++;
}else{
// 若对象中没有这个属性
// 新增属性 并赋值为1
obj[strD[i]]=1;
}
}
return obj;
}
console.log(tjChar(strD));
复制代码
字符串对象的属性添加
var newObj = {
name : "张三"
}
复制代码
添加字符串对象属性
newObj.age = 18
console.log(newObj);
复制代码
“”为false 其他字符串都是true
判断字符串对象是否存在某个属性
if (newObj.a) {
console.log("我打印了!");
}
if (newObj["age"]) {
console.log("我悟到了");
}
复制代码
字符串API总结
字符串转数组 split();
字符串是否包含某个字符 includes() indexOf()
字符串转为Unicode编码 charCodeAt()
将Unoicode编码转换回字符串 String.fromCharCode()
字符串替换 replace("old","new")
截取字符串 slice("startIndex","endIndex") 不包括 endIndex
若是负数 从倒数第几位一直到最后
var str = "1234567";
console.log(str.slice(-3));
返回指定字符串的索引 indexOf("searchChar","pos")
参数1 要查找的字符 参数2 开始查找的位置
通过索引找到对应的字符 charAt("index");
复制代码
数组和字符串都有的API总结
includes() indexOf() concat()【连接2个字符串或者数组】 slice()
数组转为字符串 toString() join("指定分隔符")
字符串转数组 split("指定分隔符")
复制代码
concat()【连接2个字符串或者数组】
var strF = "你好";
var strY = "世界";
var resG = strF.concat(strY);
console.log(resG);
var arrF = [1,2,3];
var arrY = [4,5,6];
var resGG = arrF.concat(arrY);
console.log(resGG);
复制代码
G.OBJECT其他对象
对象的遍历
// 创建一个对象
var obj = {
name:"打工人",
age : 22,
sex : "男",
address : "江苏南京",
tel:123456
}
/* for in 遍历对象 */
for(key in obj){
console.log(key);//打印键
console.log(obj[key]);//打印值
}
复制代码
在对象中添加元素属性 如果添加的元素属性存在 则重新赋值 如果不在 就添加新的元素
obj.hobby = "敲代码";
console.log(obj.hobby);
复制代码
删除对象中的元素属性
delete obj.hobby;
console.log(obj.hobby);
复制代码
(满满的干货,来之不易,希望大家不要白嫖,留下你们的赞和关注!!)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END