JavaScript中的对象
//自定义对象的创建
//使用object创建一个对象
//使用object创建一个对象
var student=new Object()
//document.write(student)
//给student添加属性stuID,stuName,className
student.stuID="10001"
student.stuName="张三"
student.className="移动2班"
//给对象添加一个函数sayHello()
student.sayHello=function(){
//console.log("大家好")
document.write("大家好")
}
//console.log(student)
//对象名.函数名()实现函数的调用
student.sayHello()
document.write(student.stuID)
var student=new Object()
//给student添加属性stuID,stuName,className
student.stuID="10001"
student.stuName="张三"
student.className="移动2班"
//给对象添加一个函数sayHello()
student.sayHello=function(){
document.write("大家好")
}
student.sayHello()
document.write(student.stuID)
//使用function创建一个对象
function teacher(tid,tname){
//this表示当前对象
this.tid=tid//教师编号
this.tname=tname//教师姓名
this.eat=function(){
document.write("吃饭")
}
}
var t1=new teacher("1","李四")
//使用t1来访问属性和函数
//调用函数
t1.eat()
document.write(t1.tid,t1.tname)
//使用function创建一个对象
function teacher(tid,tname){
this.tid=tid//教师编号
this.tname=tname//教师姓名
this.eat=function(){
document.write("吃饭")
}
}
var t1=new teacher("1","李四")
//使用t1来访问属性和函数
//调用函数
t1.eat()
document.write(t1.tid,t1.tname)
用对象名.函数名()实现函数的调用
JavaScript内置对象
字符串对象
(用于存储一系列字符,使用单引号或双引号包含)
数学对象
(用于获取各种数学常量及数学函数)
日期对象
(用于获取或操作各种时间)
字符串对象
length:返回字符串长度
charAt(num):返回参数num指定索引处的字符
charCodeAt(num):返回参数num指定索引处的字符的Unicode
indexOf(string[num]):返回参数string在字符串中首次出现的位置
substring(index1,[index2]):返回字符串index1与index2之间的字符串//[0,5)
substr(index,[num]):返回字符串中index1之后的num个字符串
replit(reg,string):将字符串替换为新的字符串
//substr():第一个参数指的是索引,第二个参数指的是截取的字符串的长度