JavaScript二 Li.067
JavaScript 面向对象, 类的定义和使用 定义格式 class 类名{ constructor(变量列表){ 变量赋值; } 方法名(参数列表){ 方法体; return 返回值; } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>类的定义和使用</title> </head> <body> </body> <script> class Person{ constructor(name,age) { this.name = name this.age = age } // show method show(){ document.write(this.name+" "+this.age+"<br/>"); } eat(){ document.write("吃饭<br>"); } } let p = new Person("张三",23) p.show() p.eat() </script> </html> 字面量定义类和使用 let 对象名 = { 变量名 : 变量值, 变量名 : 变量值, ... 方法名 : function(参数列表){ 方法体; return 返回值; } ... }; 使用格式 对象名.变量名 对象名.方法名(); <script> let person = { name : "张三", age : 23, hobby : ["听课","学习"], show : function (){ document.write(this.name+" "+this.age+"<br>") }, eat : function (){ document.write("吃饭") } } document.write(person.name+person.age+person.hobby[0]+","+person.hobby[1]+"<br/>") person.eat() </script> 继承 继承: 让类与类产生子父类的关系,子类可以使用父类有权限的成员 继承关键字: extends 顶级父类: Object <script> class Person{ constructor(name,age) { this.name = name this.age = age } // show method show(){ document.write(this.name+" "+this.age+"<br/>"); } } class Worker extends Person{ constructor(name,age,salary) { super(name, age); this.salary = salary } show(){ document.write(this.name+this.age+this.salary) } } var w = new Worker("张三",23,10000) w.show() </script JavaScript 内置对象 Number 方法名 说明 parseFloat() 将传入的字符串浮点数转为浮点数 parseInt() 将传入的字符串整数转为整数 <script> // 1. parseFloat() 将传入的字符串浮点数转为浮点数 document.write(Number.parseFloat("2.333")) document.write("<br>") document.write(Number.parseFloat("2.333")+2) document.write("<br>") // 2. parseInt() 将传入的字符串整数转为整数 document.write(Number.parseInt("100")) document.write("<br>") document.write(Number.parseInt("100")+20) document.write("<br>") document.write(Number.parseInt("1100ab")) document.write("<br>") </script> Math 方法名 说明 ceil(x) 向上取整 floor(x) 向下取整 round(x) 把数四舍五入为最接近的整数 random() 随机数,返回的是0.0-1.0之间范围(含头不含尾) pow(x,y) 幂运算的x的y次方 <script> // ceil(x) 向上取整 document.write(Math.ceil(4.4)) document.write("<br>") // floor(x) 向下取整 document.write(Math.floor(4.4)) document.write("<br>") // round(x) 把数四舍五入为最接近的整数 document.write(Math.round(4.1)) document.write("<br>") document.write(Math.round(4.6)) document.write("<br>") // random() 随机数,返回的是0.0-1.0之间范围(含头不含尾) document.write(Math.random()) document.write("<br>") // pow(x,y) 幂运算的x的y次方 document.write(Math.pow(2,3)) </script> Date 构造方法 说明 Date() 根据当前时间创建对象 Date(value) 根据指定毫秒值创建对象 Date(year,month,[day,hours,minutes,seconds,milliseconds]) 根据指定字段你出去对象(月份是0-11) 成员方法 说明 getFullYear() 获取年份 getMonth() 获取月份 getDate() 获取天数 getHours() 获取小时 getMinutes() 获取分钟 getSeconds() 获取秒数 getTime() 返回距1970年1月1日至今的毫秒数 toLocaleString() 返回本地日期格式的字符串 <script> // Date() 根据当前时间创建对象 let d1 = new Date() document.write(d1) document.write("<br>") // Date(value) 根据指定毫秒值创建对象 let d2 = new Date(20000) document.write(d2) document.write("<br>") // Date(year,month,[day,hours,minutes,seconds,milliseconds]) 根据指定字段你出去对象(月份是0-11) let d3 = new Date(2022,9,3,4,5,6) document.write(d3) document.write("<br>") // getFullYear() 获取年份 document.write(d3.getFullYear()) document.write("<br>") // getMonth() 获取月份 document.write(d3.getMonth()) document.write("<br>") // getDate() 获取天数 document.write(d3.getDate()) document.write("<br>") // getHours() 获取小时 document.write(d3.getHours()) document.write("<br>") // getMinutes() 获取分钟 document.write(d3.getMinutes()) document.write("<br>") // getSeconds() 获取秒数 document.write(d3.getSeconds()) document.write("<br>") // getTime() 返回距1970年1月1日至今的毫秒数 document.write(d3.getTime()) document.write("<br>") // toLocaleString() 返回本地日期格式的字符串 document.write(d3.toLocaleString()) document.write("<br>") </script String 内置对象 构造方法 说明 String(value) 根据指定字符串创建对象 let s="字符串" 直接赋值 成员方法 说明 length属性 获取字符串长度 charAt(index) 获取指定索引处的字符 indexOf(value) 获取指定字符串出现的索引位置,换不到为-1 substring(start,end) 根据指定索引范围截取字符串(含头不含尾) split(value) 根据指定规则切割字符串,返回数组 replace(old,new) 使用新字符串替换老字符串 <script> // String(value) 根据指定字符串创建对象 let s1 = new String("Hello") document.write(s1) document.write("<br>") // let s="字符串" 直接赋值 let s2 = "World" document.write(s2) document.write("<br>") // 成员方法 说明 // length属性 获取字符串长度 let s3 = "What is you name!" document.write(s3.length) document.write("<br>") // charAt(index) 获取指定索引处的字符 document.write(s3.charAt(10)) document.write("<br>") // indexOf(value) 获取指定字符串出现的索引位置,换不到为-1 document.write(s3.indexOf("a")) document.write("<br>") // substring(start,end) 根据指定索引范围截取字符串(含头不含尾) document.write(s3.substring(0,4)) document.write("<br>") // split(value) 根据指定规则切割字符串,返回数组 let s5arr = s3.split("\\s") for(ss of s5arr){ document.write(ss) } document.write("<br>") // replace(old,new) 使用新字符串替换老字符串 let s6 = "What is youuuu name!" document.write(s6.replace("uu","s")) document.write("<br>") </script> RegExp ...