自定义对象的创建

<!-- text文本框  button单选按钮 -->
		第一个数:<input type="text" name="" id="one" value="" /><br />
		第二个数:<input type="text" name="" id="two" value="" /><br />
		运算方式:
		<input type="button" name="ys" id="ys" value="+" onclick="cal('+')"/>
		<input type="button" name="ys" id="ys" value="-" onclick="cal('-')"/>
		<input type="button" name="ys" id="ys" value="*" onclick="cal('*')"/>
		<input type="button" name="ys" id="ys" value="/" onclick="cal('/')"/><br />
		                                   <!-- onclick="cal()" 是单击按钮是点击事件-->
		运算结果:
		<input type="text" name="" id="result" value="" /><br />
		<script type="text/javascript">
			function cal(y){
				//实现两个数相加
				var one =document.getElementById("one").value
				var two =document.getElementById("two").value
				var result=0 
				if (y=="+") {
					result=parseFloat(one)+parseFloat(two)
				} else if (y=="-") {
					result=parseFloat(one)-parseFloat(two)
				} else if (y=="*") {
					result=parseFloat(one)*parseFloat(two)
				}else if (y=="/") {
					result=parseFloat(one)/parseFloat(two)
				}
				// 也可以使用Switch...case来实现
				document.getElementById("result").value=result
			}
		</script>

属性:是指对象包含的值,使用‘对象名.属性名’的方式进行操作,
方法:在代码里,使用‘对象名.方法名( )’来调用该对象的方法
事件:相应用户操作,完成交互,一般可以分为鼠标事件,键盘事件及其他事件
自定义对象:开发人员根据自己的需要而定义的新对象
JavaScript内置对象:JavaScript将一些常用功能预先定义成对象,用户可以直接使用。如字符串对象,数学对象,日期对象,数组对象,History对象等

 自定义对象的创建,可以使用object创建,使用function创建

// 字符串对象
			var str="Hello World!"
			console.log(str.length) //length用来获取字符串的长度
			console.log(str.charAt(2)) //返回参数指定索引处的字符
			// 返回参数在字符串中首次出现的位置
			console.log(str.indexOf("l"))
			console.log(str.indexOf("b"))
			// 判断是否包含某个字符
			console.log(str.substring(0,5))  // 0 1 2 3 4 不包含第五个
			console.log(str.substr(1,2)) 
			// 从第一个索引值开始 返回两个值 
			// substr():第一个参数指的是索引 第二个参数指的是截取的字符串的长度
			

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值