内置对象
三种内置对象:自定义对象,内置对象,浏览器对象
JS语言自带的一些对象,提供一些常用或是最基本而非必要的功能。
eg:math date array string
查文档
MDN
查阅方法:
- 查阅该方法的功能
- 查看里面参数的意义和类型
- 查看返回值的意义和类型
- 通过demo测试
Math
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
console.log(Math);
console.log(Math.PI);//输出为π
console.log(Math.floor(1.9));//向下取整
console.log(Math.ceil(1.1));//向上取整
console.log(Math.round(4.3));//四舍五入
console.log(Math.abs(-33));//取绝对值
console.log(Math.max(1, 2, 3, 4, 5));//取最大值
console.log(Math.max.apply(null, [1, 2, 3, 4, 5]));//null表示this指针不替换
console.log(Math.min(1, 2, 3, 4, 5));//取最小值
console.log(Math.min.apply(null, [1, 2, 3, 4, 5]));
console.log(Math.sqrt(100));//开平方
console.log(Math.pow(5, 3));//幂,5的3次方
console.log(Math.sin(90 * Math.PI / 180));//正弦,弧度=角度*π/180
console.log(Math.cos(180 * Math.PI / 180));//余弦
console.log(Math.tan(90 * Math.PI / 180));//正切
</script>
</body>
</html>
案例:猜数字游戏
<script>
function getRandom(min,max){
return Math.floor(Math.random()*(max-min+1))+min;
}
var random=getRandom(1,10);
while(true){
var num=prompt(1-10);
if(num>random){
alert('big');
}else if(num<random){
alert('small');
}else{
alert('right');
break;
}
Date
案例:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
/*获取时间*/
var date = new Date();
console.log(date);//输出时间格式:Mon Dec 28 2020 10:11:14 GMT+0800 (中国标准时间)
//对输出时间的默认格式进行转化的方法
console.log(date.toDateString());
console.log(date.toISOString());
console.log(date.toLocaleDateString());
console.log(date.toLocaleString());
console.log(date.toTimeString());
console.log(date.toUTCString());
//获取具体时间的方法
console.log(date.getDate());//日
console.log(date.getDay());//周
console.log(date.getFullYear());//年
console.log(date.getYear());//当前两份到1900年总年数
console.log(date.getMonth());//月(月份默认减掉了一,因为从0-11)
console.log(date.getHours());//小时
console.log(date.getMinutes());//分钟
console.log(date.getSeconds());//秒
console.log(date.getMilliseconds());//毫秒
console.log(date.getTime());//返回当前时间到1970年1月1日的总毫秒数
/*统一设置时间*/
var time = new Date("2020/12/29 00:00:00");
var time = new Date("2020-12-28 00:00:00");//字符串的设置方法比较方便,因为不涉及月份的加一减一
var time = new Date(2020, 10, 28, 0, 0, 0);
/*分开设置时间*/
var time =new Date();
time.setDate(28);//设置日
time.setFullYear(2021);//设置年
time.setMonth(1);//设置月,实际出来加一之后为2月
time.setHours(9);//设置时
time.setMinutes(0);//设置分钟
time.setSeconds(0);//设置秒
time.setMilliseconds(1000);//设置毫秒
console.log(time);
</script>
</body>
</html>
数组对象
案例:数组去重
fuction unique(arr){
var newArrr=[];
for(var i=0;i<arr.length;i++){
if(newArr.indexOf(arr[i])==-1){
newArr.push(arr[i]);
}
}
return newArr;
}
字符串对象
"abcdefg".charAt(1) // 参数: n 任意的正整数 返回值: n下标的对应字符值,n<0 n>string.length 返回空串
"abcdef".charCodeAt(n) // 返回的是n下标对应字符的Unicode编码
拼接字符串: (1) + (2)string.concat(value,.....)
var a = 'abc'
var b = 'cdefg'
var c = 'oooppp i have a apple'
/* document.write(a+b+c)*/
document.write(a.concat(b,c))
###indexof(substring 子串,start )
console.log(b.indexOf(c)) 如果不存在这个子串返回-1 省略第二个参数start默认索引为0下标
替换: 父串.replace(正则,子串)
text = "javascripthelloJavascript"
text.replace(/javascript/i, "JavaScript"); ===>"JavascripthelloJavascript"
### string.slice(start, end) 提取子串
var str = "abgdh234lkdgjrf"
str.slice(2,5)
### split(分隔符) 字符串---》数组
"2023-2-30".split('-')===>['2023','2','30']
url :
https://home.firefoxchina.cn/?name='lili'&year=23
var str =" https://home.firefoxchina.cn/?name='lili'&year=23"
undefined
str.indexOf('?')
30
str.slice(31)
"name='lili'&year=23"
var str1 = "name='lili'&year=23"
undefined
str1.split('&')
(2) ["name='lili'", "year=23"]