js内置对象总结(构造函数.object.其他方法)

发布于:2022-12-11 ⋅ 阅读:(736) ⋅ 点赞:(0)

1.字符串的索引从 0 开始,这意味着第一个字符索引值为 [0],第二个为 [1], 以此类推。

你可以在字符串中使用引号,字符串中的引号不要与字符串的引号相同

2.字符串长度

//声明一个常量
var txt = "Hello World!";
//判断一个字符串的长度
document.write( txt.length );

3.string内置对象的方法

charAt() 返回指定索引位置的字符
charCodeAt() 返回指定索引位置字符的 Unicode 值
concat() 连接两个或多个字符串,返回连接后的字符串
fromCharCode() 将 Unicode 转换为字符串
indexOf() 返回字符串中检索指定字符第一次出现的位置
lastIndexOf() 返回字符串中检索指定字符最后一次出现的位置
localeCompare() 用本地特定的顺序来比较两个字符串
match() 找到一个或多个正则表达式的匹配
replace() 替换与正则表达式匹配的子串
search() 检索与正则表达式相匹配的值
slice() 提取字符串的片断,并在新的字符串中返回被提取的部分
split() 把字符串分割为子字符串数组
substr() 从起始索引号提取字符串中指定数目的字符
substring() 提取字符串中两个指定的索引号之间的字符
toLocaleLowerCase() 根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射
toLocaleUpperCase() 根据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小

4.三种属性的书写方法

第一种:使用object

//第一种
			//自定义对象,使用object
			var student=new Object()
			//给对象设置一些属性
			student.stuID="1001"
			student.stuName="张三"
			student.calssName="移动2103班"
			
			//给对象设置函数
			student.sayhellow=function(){
				console.log("大加号")
			}
			student.sayhellow()

第二种:构造函数

//第二种:构造函数
		    function teacher(stuid,stuname){
				//构造函数中的this代表当前对象
				this.stuid=tid
				this.stuname=tname
				this.Eat=function(){
					console.log("吃饭")
				}
			}

第三种:其他方法

//第三种方法:其他方法
			var stu={
				stuid:"1002",
				stuname:"李四",
				study:function(){
					console.log("学习")
				}
			}
			stu.study()
			console.log(stu.stuid)

5.实例一:


第一个数:<input type="text" id="one" /><br />
		第二个数:<input type="text" id="two"  /><br />
		运算方式:<input type="button" name="" id="jiafa" value="+"  onclick="cal_1('+')"/>
		        <input type="button" name="" id="jianfa" value="-"  onclick="cal_1('-')"/>
				<input type="button" name="" id="chengfa" value="*"  onclick="cal_1('*')"/>
				<input type="button" name="" id="chufa" value="/"  onclick="cal_1('/')"/><br />
				运算符:<input type="text" name="result" id="result" value="" />
				<script>
					function cal_1(y){
						//获取文本框输入的值
						var one=document.getElementById("one").value
						var two=document.getElementById("two").value
						var result
						if(y=="+"){
							result=parseFloat(one)+parseFloat(two)
						}else if(y=="-"){
							result=parseFloat(one)-parseFloat(two)
						}else if(y=="*"){
							result=parseFloat(one)*parseFloat(two)
						}else{
							result=parseFloat(one)/parseFloat(two)
						}
						document.getElementById("result").value=result
					}
				</script>

案例二:

第一个数:<input type="text" id="one" value="" />+
		第二个数:<input type="text" id="two" value="" />=
		<input type="text" id="result" value="" />
		<input type="button" name="" id="" value="运算"  onclick="cal()"/>
		<script type="text/javascript">
			function cal() {
				var one=document.getElementById("one").value
				var two=document.getElementById("two").value
				var result=parseFloat(one)+parseFloat(two)
				document.getElementById("result").value=result
			}
		</script>

本文含有隐藏内容,请 开通VIP 后查看