前言
正文
Math.abs(-10); // => 10:绝对值
Math.ceil(0.6); => 1.0:向上取整数
Math.floor(0.6); => 向下取整数
Math.round(0.6); =>:1.0:四舍五入
Math.random(); => 0-1随机数
Math.max(1,3,5); => 返回最高值
Math.min(1,-3,50); => 返回最低值
Math.pow(2,3); => 8:2的3次方
Math.PI; => π:圆周率
Math.sin(3); => 3的正弦值
Math.sqrt(30); => 平方根
Math.tan(100); => 正切
Math.acos(10); => 反余弦值
Math.cos(100); => 余弦值
Math.exp(100); => e的100次幂
var str1 = "hello world";
str1.slice(1,4); "ell":截取下标1到下标4,不包含最后一位
str1.slice(1); "ello world":截取下标1以后的所有字符
str1.slice(-3); "rld":截取后三位
var exp1 = undefined;
if (exp1) {
console.log("真");
} else {
console.log("假");
}
output:假
- toFixed() => 根据小数点后指定位数将数字转化成字符串,会进行四舍五入;
- toExponential() => 使用指数计数法将数字转换为字符串;
- toPrecision() => 根据指定的有效字位数将数字转换成字符串;
var n = 123456.78;
n.toFixed(0); output:123457
n.toFixed(1); ouput:123456.8
n.toFixed(5); output:123456.78000
n.toExponential(1); output:1.2e+5
n.toPrecision(4); output:1.2346e+5
n.toPrecision(7); output:123456.8
n.toPrecision(10); output:123456.7800
parseInt("11",5); 6 => 1*5+1
parseInt("ff",16); 255 => 15*16+15
parseInt("077",10); 77 => 70*10+7
var str1 = "hello";
str2 = "world!";
console.log("%s %s",str1,str2);
output:hello world!
;
this.str3 = "!!!"delete str1; false
delete str2; true
delete str3; true
var scope = "global"function f() {
console.log(scope);
var scope = "local";
console.log(scope);
}
f();
console.log(scope);
output: undefined、local、global
f() {
var scope; 只声明变量,变量前置
console.log(scope);
scope = "local"output: undefined、local、global
var now = new Date();
typeof (now + 1); output:string
typeof (now - 1); output:number
now == now.toString(); output:true
Animal(name) {
this.name = name || 'Animal';
this.sleep = () {
console.log(this.name + '正在睡觉!');
}
}
Animal.prototype.eat = (food) {
console.log(this.name + '正在吃:' + food);
};
Cat() {
}
Cat.prototype = Animal();
Cat.prototype.name = 'cat';
var cat = Cat();
console.log(cat.name);
console.log(cat.eat('fish'));
console.log(cat.sleep());
console.log(cat instanceof Animal); true
console.log(cat instanceof Cat); true
var array = ["apple","banana","cherry","dates","fig"];
array.slice(1,4); ["banana","dates"]
array.slice(1);
array.slice(-2); ["dates","fig"]
console.log(array); ["apple","fig"]
Array.prototype.slice(x,y); 第一位参数为截取开始下标,截取的时候包含此下标,第二位参数缺省参数,如果不填写,标识截取到数组的最后一位,如果填写了,标识截取到下标的位置,截取元素不包含最后一位,截取不改变原来数组。
splice部分
splice定义:方法通过删除现有元素和/或添加新元素来更改一个数组的内容。
代码1:
方法通过删除现有元素和/或添加新元素来更改一个数组的内容。
];
array.splice(2,2); 从下标2开始截取,截取2个
console.log(array);
];
array.splice(2); 从下标2开始截取,截取到最后
console.log(array);
截取下标2到后面2个元素替换成"plum","orange"
console.log(array);
];
array.length = 3;
array.length = 0; [] => 删除所有元素
var array = [10,5,20,15];
var sum = array.reduce(function (x,y) { return x + y },0); 求和
var product = array.reduce(return x * y },0); 求积
var max = array.reduce(return (x > y) ? x : y }); 求最大值
fun(element,index,array) {
return element > 10;
}
[2,8,1,4].some(fun); false
[12,4].some(fun); true
[12,4].every(fun); true
var array = [1,4,9,16var map = array.map(x => x * 2); [2,32]
var fun = (x) {
console.log(x);
if (x < 1) {
return 1;
}
return x + arguments.callee(x - 1);
}
fun(3); 7 => 3+2+1+1
var array = [3,12,4];
array.sort(function (a,b) { return a - b; }); [1,12]
sum(x,y,f) {
return f(x) + f(y);
}
console.log(sum(-5,6,Math.abs));
let [x,y] = [1,2];
[x,y] = [x + 1,y + 1 [y,x];
console.log(x,y); 3 2
var Cat = () {
var name = "cat"var age = 2this.getName = () {
return name;
}
this.getAge = age;
}
}
Cat();
console.log(cat.name); undefined
console.log(cat.age); undefined
console.log(cat.getName()); cat
console.log(cat.getAge()); 2