我们知道数组,就是一组值的列表,该表中的每一个值都有自己的索引值,索引值从0开始,依次递增。事实上,对象与数组很相似,唯一的区别就是它的键值类型是自定义的,如name、age等。
//自定义对象的创建
//使用Object创建一个对象
var student=new Object()
//给对象添加属性stuID、stuName、className
student.stuID="1001"
student.stuName="王大炮"
student.className="移动212"
//给对象添加了一个函数sayHello
student.sayHello=function(){
console.log("大家好")
}
student.sayHello()
console.log(student.stuID)
//使用function创建一个对象
function teacher(tid,tname){
//this表示当前对象
this.tid=tid //教师编号
this.tname=tname//教师姓名
this.eat=function(){//行为
console.log("干饭")
}
}
var t1=new teacher("1","王二")
//使用t1来访问属性和函数
//调用函数
t1.eat()
console.log(t1.tid,t1.tname)
对象的属性、方法、和事件
属性:属性是指对包含的值,使用 '对象名.属性名' 的方式来进行操作,如document.myfrom.first.value
方法:在代码里,使用 '对象名.方法名()'来调用该对象的方法。
alter(")=Window(")
事件:
响应用户操作、完成交互、如onclick、OnKeyDown
一般可分为鼠标事件、键盘事件及其他事件
字符串对象
//字符串对象
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,5]
//substr():第一个参数指的是索引,第二个参数指的是截取字符串的长度
console.log(str.substr(1,2))