JavaScript对象

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():第一个参数指的是索引,第二个参数指的是截取的字符串的长度
            

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值