JavaScript基础知识(二)

本文详细介绍了JavaScript中的几种常用内置对象,包括String字符串对象的各种操作方法、Math数字对象的属性及方法、Date时间对象的获取时间的方法,以及Array数组对象的定义方式与常用方法等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaScript的几个常用内置对象

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>JS string</title>
		<script type="text/javascript">
		/**	<!-- =================================== -->
		* 	<!-- 第二阶段 -->
		*	<!-- JS的内置对象 -->
		*/
  
		/*String 字符串对象
		*1、定义字符串变量的三种方法
		*/
			var str1 = "初始化赋值";
			var str2 = new String("新建字符串对象");
			var str3 = String("请查看网页源代码<br>字符串赋值123456abcdeABCDE123456");
			alert(str1);
			console.info(str2);
			document.write(str3);
			document.write("<br>");
		/**
		*	2、String对象的length属性,返回字符串长度。
		*/
			document.write(str3.length);
			document.write("<br>");
		/**
		*	3、String对象的indexOf方法,返回指定字符在字符串中首次出现的位置。
		*	lastIndexOf()方法是从后向前。
		*	不存在则返回 -1*/
			document.write(str3.indexOf("串"));
			document.write("<br>");
			document.write(str3.lastIndexOf("3"));
			document.write("<br>");
		/**  
		*	4、字符串的 charAt()方法,用于返回字符串中指定下标位置的字符(的ASCII码)。
		*/
			document.write(str3.charAt(7));
			document.write("<br>");
		/**
		*	5、截取字符串的两个方法
		*	substr和substring如果只有一个参数,都表示从当前位置开始截取,截取到末尾
		*	    substr两个参数,表示从第一个参数的位置开始截取,截取第二个参数代表的长度
		*	    substring两个参数,表示从第一个参数的位置开始截取,截取第二个参数代表的位置。
		*/
			document.write(str3.substr(13,17));
			document.write("<br>");
			document.write(str3.substring(13,17));
			document.write("<br>");
		/**
		*	6、其它常用的 字符串的方法
		*		charCodeAt()	返回在指定的位置的字符的Unicode 编码。
		*		concat()		连接字符串。
		*		match()			找到一个或多个正则表达式的匹配。
		*		replace()		替换与正则表达式匹配的子串。
		*		search()		检索与正则表达式相匹配的值。
		*		slice()			提取字符串的片断
		*		split()			把字符串分割为字符串数组。
		*		toLowerCase()	把字符串转换为小写。
		*		toUpperCase()	把字符串转换为大写。*/
			document.write(str3.charCodeAt(9));
			document.write("<br>");
			document.write(str3.concat(str1,str2));
			document.write("<br>");
			document.write(str3.match([3-9]));
			document.write("<br>");
			document.write(str3.replace([3-9]));
			document.write("<br>");
			document.write(str3.search(0));
			document.write("<br>");
			document.write(str3.slice(3,9));
			document.write("<br>");
			document.write(str3.split(3,3));
			document.write("<br>");
			document.write(str3.toLowerCase());
			document.write("<br>");
			document.write(str3.toUpperCase());
			document.write("<br>");

		/**
		*Math 数字对象
		*	Math 对象属性*/
				document.write(Math.E);		//返回自然常量 e。
				document.write("<br>");
				document.write(Math.PI);		//返回圆周率(约等于3.14159)。
				document.write("<br>");
		//	Math 对象的常用方法
				document.write(Math.abs(-9));		//返回数的绝对值。
				document.write("<br>");
				document.write(Math.ceil(0.1));		//对数进行上舍入。
				document.write("<br>");
				document.write(Math.floor(0.9));		//对数进行下舍入。
				document.write("<br>");
				document.write(Math.max(2,7));		//返回 x 和 y 中的最大值。
				document.write("<br>");
				document.write(Math.min(2,7));		//返回 x 和 y 中的最小值。
				document.write("<br>");
				document.write(Math.pow(1,7));		//返回 x 的 y 次幂。
				document.write("<br>");
				document.write(Math.random());		//返回 0 ~ 1 之间的随机数。
				document.write("<br>");
				document.write(Math.round(0.5));		//把数四舍五入为最接近的整数。
				document.write("<br>");
				document.write(Math.round(0.49));	//把数四舍五入为最接近的整数。
				document.write("<br>");
				document.write(Math.sqrt(9));		//返回数的平方根。
				document.write("<br>");

		/**Date 时间对象
		*	Date 对象的常用方法*/
			var myDate = new Date();
			document.write(myDate);						//返回当日的日期和时间。
			document.write("<br>");
			document.write(myDate.getDate());			//从 Date 对象返回一个月中的某一天 (1 ~ 31)。
			document.write("<br>");
			document.write(myDate.getDay());			//从 Date 对象返回一周中的某一天 (0 ~ 6)。
			document.write("<br>");
			document.write(myDate.getMonth());			//从 Date 对象返回月份 (0 ~ 11)。
			document.write("<br>");
			document.write(myDate.getFullYear());		//从 Date 对象以四位数字返回年份。
			document.write("<br>");
			document.write(myDate.getHours());			//返回 Date 对象的小时 (0 ~ 23)。
			document.write("<br>");
			document.write(myDate.getMinutes());		//返回 Date 对象的分钟 (0 ~ 59)。
			document.write("<br>");
			document.write(myDate.getSeconds());		//返回 Date 对象的秒数 (0 ~ 59)。
			document.write("<br>");
			document.write(myDate.getMilliseconds());	//返回 Date 对象的毫秒(0 ~ 999)。
			document.write("<br>");
			var datestr = myDate.toString();
			document.write(Date.parse(datestr));	//返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
			document.write("<br>");
			document.write(myDate.toString());			//把 Date 对象转换为字符串。
			document.write("<br>");
			document.write(myDate.toTimeString());		//把 Date 对象的时间部分转换为字符串。
			document.write("<br>");
			document.write(myDate.toDateString());		//把 Date 对象的日期部分转换为字符串。
			document.write("<br>");
			document.write(myDate.getTime());					//返回 1970 年 1 月 1 日至今的毫秒数(时间戳)。
			document.write("<br>");
		/**Array 数组对象
		*	1、定义数组的方法*/
			var arr1 = [];				//声名一个空数组
			var arr2 = [1,2,3,4,5]; //初始化赋值数组
			var arr3 = new Array(); //使用Array()构造空数组
			var arr4 = new Array(10); //使用Array()构造指定元素个数的数组
			var arr5 = new Array(1,2,3,4,5,6); //使用Array()构造指定内容的数组
		// 可以通过循环遍历数组
			for (var i = 0; i < arr5.length; i++) {		//length是数组的常用属性
				document.write(arr5[i]);		//通过数组的下标循环进行遍历
				document.write("<br>");
			};
			arr5[6] = 7;		//通过下标对数组下标位置的元素进行赋值
		//  2、数组的常用方法
			document.write(arr2.concat("a",arr5));			//把两个或更多的数组或值追加到数组对象中。
			document.write("<br>");
			document.write(arr2.join("/"));					//把数组的所有元素通过指定的分隔符(参数)进行分隔,组成一个字符串。
			document.write("<br>");
			document.write(arr2.pop());						//删除并返回数组的最后一个元素,数组为空则返回undefined 值。
			document.write("<br>");
			document.write(arr2.push("a","s","d","f","g"));			//向数组的末尾添加一个或多个元素,并返回新的长度。
			document.write("<br>");
			document.write(arr2.reverse());					//颠倒数组中元素的顺序。
			document.write("<br>");
			document.write(arr2.shift());					//删除并返回数组的第一个元素,数组为空则返回undefined 值。
			document.write("<br>");
			document.write(arr2.unshift("k","l","m"));				//向数组的开头添加一个或更多元素,并返回新的长度。
			document.write("<br>");
			document.write(arr2.slice(2,7));					//从数组对象返回选定的元素,切片
			document.write("<br>");
			document.write(arr2.sort());					//对数组的元素进行排序
			document.write("<br>");
			document.write(arr2.splice(2,5,"q","w","e","r"));		//删除起始(参数1)索引到结束参数2索引的值,并从参数1的位置添加新元素()参数3;。
			document.write("<br>");
			document.write(arr2.toString());				//把数组转换为字符串,并返回。
			document.write("<br>");
			
		</script>
	</head>
	<body>

	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值