JavaScript对象

一、 JavaScript中的对象

 JavaScript把对象定义为:“无序属性的集合,其属性可以包含基本值、对象或者函数。”

1、对象的属性和方法
属性、方法和事件
●属性
 属性是指对象包含的值,使用’对象名,属性名’的方式进行操作

document.myfrom.first.value
●方法
 在代码里,使用’对象名.方法名()’来调用该对象的方法。
 alter( " )=Window.alter( " )
●事件
 响应用户操作、完成交互,如OnClick、OnKeyDown
 —般可以分为鼠标事件、键盘事件及其他事件

对象的属性也可以是一个函数,因为函数本身也是一种数据,在这种情况下我们会称该属性为方法,例如:

var student={         //创建对象 
 "name":"张三",
 "age":18,    
 "eat":function(){     //方法 
    return"我是吃饭的方法"
},
 "sleep":function(){     //方法 
    return"我是睡觉的方法"}
}

二、 JavaScript用户自定义对象

1、使用Object关键字构造对象

<script type="text/javascript">
//创建自定义对象
			//使用Object创建
			var student = new Object();
			//给学生添加一些属性
			student.stuId = "221214";
			student.stuName = "张三";
			student.className = "2105班";
			student.sayHello = function(){
				document.write("大家好");
			}
			student.sayHello();
			console.log("该学生学号: " + student.stuId +",姓名 :" + student.stuName +",班级: " +student.className);
</script>

2、构造器函数

可以使用function关键字来构造一个自定义的对象类型。

<script type="text/javascript">
//使用function来创建对象
			function tescher(tid,name){
				this.tid = tid;
				this.name = name;
				this.Eat = function (){
					document.write("吃饭");
				}
			}
			
			var t1 = new teacher("1","赵丽丽");
			t1.Eat();
			document.write(t1,name);
</script>

三、 JavaScript常用对象

1、字符串对象的常用属性和方法
length
返回字符串长度
charAt(num)
返回参数num指定索引处的字符
charCodeAt(num)
返回参数num指定索引处的字符的Unicode值
indexOf(string[,num])
返回参数string在字符串中首次出现的位置
lastlndexOf(string[,num])
返回参数string在字符串中最后出现的位置
substring(index1[,index2])
返回字符串中index1和index2之间的字符串
substr(index[,num])
返回字符串中index1之后的num个字符
toUpperCase()
返回字符串大写形式
toLowerCase()
返回字符串小写形式
split(reg,num)
根据参数传入的正则表达式或者字符(串),将字符串分割成字符串数组
replace(reg,string)
根据参数传入的正则表达式或者字符(串),将字符串替换为新字符串
search(string)
返回参数string出现的位置

(1)indexOf()方法

indexOf("子字符串")方法返回一个整数值,表示string字符串对象内第1次出现子字符串的位置(索引值)。

(2)charAt()方法

charAt方法从字符串对象中返回单个字符,使用时通常会设置一个起始位置的参数,然后返回位于该位置的字符值。

(3)字符串截取的常用方法

常用的字符串截取函数有 slice()、substr()、substring()。

//字符串对象
			//获取字符串的长度使用length属性
			var str = "hello world";
			document.write("该字符串的长度是:" + str.length);
			document.write(str.charAt(1));  //返回的是指定索引处的字符
			document.write(str.indexOf("o"));//返回的是指定字符首次出现的位置
			document.write(str.indexOf("a")));//返回-1
			document.write(str.substring(0,4));//【0,4】从下标0开始截取,到索引末4结束,但取不到索引为4的字符
			document.write(str.substr(0,4));//截取的是索引0开始,截取4个字符
			document.write(str.toUpperCase());
			document.write(str.toLowerCase());
			
			var str1 = "abcdshxakjdwef";
			document.write(str1.split("d"));
			document.write(str1.replace("d","*"));//替换的是满足条件的第一个字符
			document.write(str1.search("d"));//返回的也是检索出的满足条件的第一个字符

2、Math对象的常用属性和方法
 Math是一个内部对象,提供基本的数学函数和常数

使用Math对象的random()方法来产生随机数,取值范围是[0,1) 

//Math 对象
			//返回一个数的绝对值
			document.write(Math.abs(-10));//10
			document.write("<br>");
			//返回大于或等于一个给定数的最小整数
			document.write(Math.ceil(6.5)); //7
			document.write("<br>");
			//返回一个值的余弦值
			document.write(Math.sin(90 * Math.PI /180)); //1
			document.write("<br>");
			//返回小于或等于一个给定数字的最大整数
			document.write(Math.floor(4.3)); //4
			document.write("<br>");
			document.write(Math.floor(8.9)); //8
			document.write("<br>");
			
			document.write(Math.max(1,32,34,5)); //34
			document.write("<br>");
			
			document.write(Math.pow(2,3)); //2的3次方  8
			document.write("<br>");
			
			document.write(Math.round(14.8)); //15  四舍五入
			document.write("<br>");

2、Date对象的常用属性和方法

<span id="time"></span>
	<script type="text/javascript">
			
			  //定时器
			  setInterval(function(){
				  var date = new Date();
				  var year = date.getFullYear();//年
				  var month = date.getMonth() + 1;//月
				  var day = date.getDate();//日
				 
				  var hours = date.getHours(); //时
				  var minutes = date.getMinutes();//分
				  var seconds = date.getSeconds();//秒
				 
				 month =month>9?month:"0" + month;
				 day =day>9?day:"0" + day;
				 hours =hours>9?hours:"0" + hours;
				 minutes =minutes>9?minutes:"0" + minutes;
				 seconds =seconds>9?seconds:"0" + seconds;
				
				 var result = year + "年" +month + "月" +day + "日   " +hours + "时" +minutes + "分" + seconds + "秒";
				 document.getElementById("time").innerHTML = result;
			  },1000);
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃汉堡的代码人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值