es6(二)

这几天又整理了一些es6的东西,准备放上来。


//二进制 0b开头
//八进制 0o开头
//大小写都可以
{
console.log(0b111110111); //503
console.log(0o767); //503
}

//isFinite判断值是不是有尽
{
console.log("15",Number.isFinite(15));
console.log("NaN",Number.isFinite(NaN));
console.log("1/0",Number.isFinite("true"/0)); //分母是0的情况
}

//判断是不是数
{
console.log("NaN",Number.isNaN(NaN)); //true
console.log("0",Number.isNaN(0)); //false
}

//判断是不是整数
{//接收的参数一定要是一个数
console.log("25",Number.isInteger(25)); //true
console.log("25.0",Number.isInteger(25.0)); //true
console.log("25.1",Number.isInteger(25.1)); //false
console.log("25",Number.isInteger("25")); //false
}

//判断在ab之间,不包括端点 -2的53次方 2的53次方
//MAX_SAFE 就是个常数 相当于math.pa
{
console.log(Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER);
}

//判断数是不是安全的
//参数一定要是一个数
{
console.log("10",Number.isSafeInteger(10)); //true
console.log("a",Number.isSafeInteger("a")); //false
}

//返回小数的整数部分
{
console.log(4.1,Math.trunc(4.1)); //4
console.log(4.9,Math.trunc(4.9)); //4
}

//判断一个数是正数,负数,还是0
//通过返回-1 0 1 进行判断
//参数一定要是数字
{
console.log("-5",Math.sign(-5)); //-1
console.log("0",Math.sign(0)); //0
console.log("5",Math.sign(5)); //1
console.log("foo",Math.sign("foo"));//NaN
}


//立方根
{
console.log("-1",Math.cbrt(-1));
console.log("8",Math.cbrt(8)); //2
}

 

//数组拓展
{
let arr = Array.of(3,4,7,9,11);
console.log("arr=",arr);

let empty = Array.of();
console.log("empty",empty);
}

//把伪数组转换成真数组——Array.from——把集合转义成真正的数组
//另一个作用是映射
{
let p =document.querySelectorAll("p");
let pArr=Array.from(p);
pArr.forEach(function(item){
console.log(item.textContent);
});
console.log(Array.from([1,3,5],function(item){return item*2}));//遍历过后每个数*2——2,6,10
}

//填充数组——换血——fill
{
console.log("fill-7",[1,"a",undefined].fill(7));
console.log("fill,pos",["a","b","c"].fill(7,1,3));
}

//遍历
{
//取得索引
for(let index of["1","c","ks"].keys()){
console.log("keys",index);
}
//取得值
for(let value of["1","c","ks"].values()){
console.log("values",value);
}
//取得索引+值——entries
for(let [index,value] of["1","c","ks"].entries()){
console.log("values",index,value);
}
}

//在当前数组内部把指定位置成员复制到其他位置
//三个参数:从哪个位置开始替换,从哪个位置开始读取,从那个位置截止
{
console.log([1,2,3,4,5].copyWithin(0,3,4)); //4,2,3,4,5
}


//find & find
//find只找出第一个符合条件的成员
{
console.log([1,2,3,4,5,6].find(function(item){return item>3}));
}
//返回的是下标
{
console.log([1,2,3,4,5,6].find(function(item){return item>3}));
}

//包括数字吗
{
console.log("number",[1,2,NaN].includes(NaN)); //能找到NaN
}

 

 

//函数拓展
//8.1参数默认值
//默认值后面不能再有新的变量,GG
{
function test(x,y = "world"){
console.log("默认值",x,y);
}
test("hello");
test("hello","Kill");
}
//8.2作用域
{
let x ="test";
function test2(x,y=x){
console.log("作用域",x,y); //作用域 kill kill
}
test2("kill");
//y取得是参数里的x值
}


//如果函数参数里没有对应的,会取外面的
{
let x ="test";
function test2(c,y=x){
console.log("作用域",x,y);//作用域 test test
}
test2("kill");
}


//rest参数
//在你不确定多少个参数时,把一系列参数转化成数组。
//和es5的arguments异曲同工
//不会有arguments第一个参数的问题
//rest后面不能有别的参数了
{
function test3(...arg){
for(let v of arg){
console.log("rest",v);
}
}
test3(1,2,3,4,"a");
}


//拓展运算符
//把数组拆成了值 [1,2,4]——1 2 4
//和rest相反
{
console.log(...[1,2,4]);
console.log("a",...[1,2,4]);
}


//箭头函数
//是一个函数 arrow函数名
//前面是参数 用等号尖括号进行连接 后面是返回值
{
let arrow = v => v*2;
console.log("arrow",arrow(3));
}
//如果没有参数
{
let arrow2 = () => 5;
console.log(arrow2());
}

 


//尾调用
//函数的最后一句话是不是一个函数
//尾调用的好处:提升性能【不断嵌套函数】
{
function tail(x){
console.log("tail",x);
}
function fx(x){
return tail(x);
}
fx(123);
}

 

 

//对象拓展
{
//简洁表示法
//对象里面有属性
let o =1;
let k =2;
let es5 ={
o:o,
k:k
};
let es6={
o,
k
};
console.log(es5,es6);

//对象里面有方法
//直接去掉:function
let es5_method={
hello:function(){
console.log("hello");
}
};

let es6_method={
hello(){
console.log("hello");
}
};
console.log(es5_method.hello(),es6_method.hello());
}

 

//属性表达式
{
let a = "b";
let es5_obj={
a:"c",
b:"c"
};

let es6_obj={
[a]:"c"
//[]表达式,[a]相当于es5里面b:"c"
}
console.log(es5_obj,es6_obj);
}
//{a: "c", b: "c"} a:"c" b:"c"
//{b:"c"} b:"c"

 

 

//object新增的几个api

//❤1判断两个值是否相等——Object.is——功能和===没区别

{
console.log("字符串",Object.is("abc","abc"),"abc"==="abc");

//数组引用的是两个不同的地址,严格意义讲,不相等
console.log("数组",Object.is([],[]),[]===[]);

}

 

//❤2拷贝——合并成一个对象——是浅拷贝【只修改引用地址,而不是拷贝所有值过去】
//不会拷贝继承和不可枚举的属性
{
console.log("拷贝",Object.assign({a:"a"},{b:"b"}));
//从{a:"a"} {b:"b"}——变成 {a:"a",b:"b"}
}

posted @ 2017-10-28 09:29  Jqy_姜浅予  阅读(114)  评论(0编辑  收藏  举报