match对象
| 方法 | 方法描述 |
|---|---|
| Math.abs(x) | 返回x的绝对值。 |
| Math.round(x) | 返回x四舍五入之后的整数值 |
| Math.ceil(x) | 返回x的近似值,向上取整 |
| Math.floor(x) | 返回x的近似值,向下取整 |
| Math.max(x,y) | 返回x,y中的最大值 |
| Math.min(x,y) | 返回x,y中的最小值 |
| Math.random() | 返回一个0~1之间的数字 |
| Math.trunc(x) | 将x的小数部分去除,返回整数部分(ie不能使用) |
| Math.pow(x,y) | 取x的y次幂 |
| Math.sqrt(x) | 返回x的平方根 |
| Math.sin(x) | 返回x的正弦值 |
| Math.cos(x) | 返回x的余弦值 |
| Math.tan(x) | 返回x的正切值 |
| Math.asin(x) | 返回x的反正弦值 |
| Math.acos(x) | 返回x的反余弦值 |
| Math.atan(x) | 返回x的反正切值 |
| NumberObject.toFixed(num) | 可把 Number 四舍五入为指定小数位数的数字 |
class
es6 新增的class类其实是构造函数的语法
语法:class 类名{}
详解:
-
构造函数可以不实例化执行,而类必须实例化
class cat{} var o = new cat(); -
不管是构造函数还是类,赋值给新的变量,应该用新变量
var fun = class cat{} var o = new fun(); var o = new cat(); //报错 -
class类里面必须采用es6语法
-
每一个类都有constructor()构造函数,constructor返回
当前的实例化对象this
class cat{ constructor(){ this.name = "张三" } } -
class类属性必须显式的写在constructor,原型里面添加方法
需写在constructor后面
class cat{ constructor(){ this.name = "张三" } // 添加到原型身上的方法 eat(){ return "吃饭" } } var o = new cat(); -
不存在变量提升(在全局环境中遇到var/function,提前解析到内存中,class不会提前解析到内存中)
var o = new cat(); // cat报错 class cat{} -
如果传递参数在constructor里面接收
class cat{ constructor(x){ console.log(x); // 10 console.log(arguments); } } var o = new cat(10) -
可以立即执行
var o = new class{ constructor(){ this.name = "张三" } }() // 传参 var o = new class{ constructor(x){ this.name = "张三"; this.age = x; } }(10) -
静态方法只有类本身和他的子类可以调用,实例化的对象不可以调用
class cat{ constructor(){ this.name = "张三" } static eat(){ return "吃" } play(){ return this.name+"玩"+cat.eat() } } var o = new cat(); console.log(o); console.log(o.play()); // 玩 console.log(o.eat()); // 静态方法 console.log(cat.eat()) //可在在外面调用静态方法
类继承
es6 类继承通过关键字extends 来实现继承,在继承的同时
需要在constructor里面调用supper()
// 子承父业
class father{
constructor(){
this.lastname="赵";
this.work = ()=>{
return "厨神"
}
}
}
class son extends father{
constructor(){
super();
this.firstname="赵99"
}
}
var $son = new son();
console.log($son);
console.log($son.lastname)
console.log($son.work())
toFixed() 方法
toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。
语法
NumberObject.toFixed(num)