ES6(2)

目录

 

8. Es6中的函数和数组补漏

9. Es6中的对象

10. Symbol在对象中的作用(对元素的保护作用)

11. set和WeakSet数据结构

​12. map数据结构

​13. proxy 代理


8. Es6中的函数和数组补漏


//对象的函数结构json
let json ={
    a:'vivi',
    age:18,
    b:"薇薇"
}
function fun({a,b}){
    console.log(a,b)//vivi 薇薇
}
fun(json)
//数组结构
let arr  = ['vivi','bibi','yoyo'];
function fun(a,b,c){
    console.log(a,b,c);//vivi bibi yoyo
}
fun(...arr);

in 的用法

//in 的用法
//判断数组或者对象中是否含有某个值
let obj = {
    a:'vivi',
    b:'bibi'
}
console.log('c' in obj);//false


let arr =[,,'vivi',]
console.log(0 in arr);//false
console.log(2 in arr);//true

数组遍历

let arr =['bibi','yoyo','vivi']
arr.forEach((v,i)=>console.log(v+'---'+i))
arr.filter(x=>console.log('fileter',x))
arr.some(y=>console.log('some',y))
arr.map(z=>console.log('map','didi'))

ES6(2)_第1张图片数组转字符串

let arr =['bibi','yoyo','vivi'];
console.log(arr.toString());//bibi,yoyo,vivi
console.log(arr.join('|'));//bibi|yoyo|vivi

9. Es6中的对象


//对象的赋值
let name = 'vivi';
let age = 18;
let json = {
    name,age
}
console.log(json);//{name: "vivi", age: 18}
//key值的构建
let a = 'name';
let json = {
    [a]:'vivi'
}
console.log(json);//name:'vivi'
//自定义对象的方法
let obj = {
    add:function(a,b){
        return a+b
    }
}
console.log(obj.add(3,6));//9

is()两个对象的比较

let obj = {name:'vivi'};
let obj1 = {name:'vivi'};
console.log(obj.name===obj1.name);//true
console.log(Object.is(obj.name,obj1.name));//true
//===同值相等 is()严格相等
console.log(+0===-0);//true
console.log(NaN===NaN);//false
console.log(Object.is(+0,-0));//false
console.log(Object.is(NaN,NaN));//true

assign合并对象(浅拷贝)

let a = {a:'vivia'};
let b = {b:'vivib'};
let c = {c:'vivic'};
let d = Object.assign(a,b,c);
console.log(d);//{a: "vivia", b: "vivib", c: "vivic"}

10. Symbol在对象中的作用(对元素的保护作用)


let vivi = Symbol('薇薇');
console.log(vivi);
console.log(vivi.toString());

let vivi = Symbol();
let json = {
    [vivi]:'web'
}
console.log(json[vivi]);//web
json[vivi]='tangtang';
console.log(json[vivi]);//tangtang
let obj = {name:'vivi',age:18};
let kill = Symbol();
obj[kill]='web'
for(let item in obj){
    console.log(item)//name age
}
console.log(obj[kill]);//web

11. set和WeakSet数据结构


//set

let setArr = new Set(['bibi','vivi','tangtang','vivi']);
console.log(setArr);//Set(3) {"bibi", "vivi", "tangtang"}
//size
console.log(setArr.size);//3
//增add
setArr.add('mimi');
console.log(setArr);//Set(4) {"bibi", "vivi", "tangtang", "mimi"}

//查has
console.log(setArr.has('vivi'));//true

//删clear
setArr.delete('vivi');
console.log(setArr);//Set(3) {"bibi", "tangtang", "mimi"}

setArr.clear();//全部删除
console.log(setArr);//Set(0) {}
// WeakSet

let weakObj = new WeakSet();
let obj = {a:'vivi',b:18};
let obj1 = {a:'vivi',b:19};
weakObj.add(obj);
weakObj.add(obj1);
console.log(weakObj);

ES6(2)_第2张图片12. map数据结构


//mab数据结构
let json = {
    name:'vivi',
    skill:'web'
}

var map = new Map();
map.set(json,'bibi');
console.log(map);

ES6(2)_第3张图片

//mab数据结构
let json = {
    name:'vivi',
    skill:'web'
}

var map = new Map();
//赋值
map.set(json,'bibi');
console.log(map);
//取值
console.log(map.get(json));//bibi
console.log(map.get('tangtang'));//{name: "vivi", skill: "web"}
//删除
map.delete('tangtang')
console.log(map);

ES6(2)_第4张图片13. proxy 代理


作用:增强 对象和函数(方法)--预处理

let obj={
    add:function(val){
        return val+100;
    },
    name:'im is vivi'
}
console.log(obj.add(100));//200
console.log(obj.name);//im is vivi


//第一个是{}对象体
//第二个是{}预处理
//get set apply
let pro = new Proxy({
    add:function(val){
        return val+100;
    },
    name:'im is vivi'
},{
    get:function(target,key,property){
        console.log('come in get')
        //target就是  name:'im is vivi'
        //key就是 name
        return target[key]
    },
    set:function(target,key,value,receiver){
        console.log(`setting ${key}=${value}`)
        return target[key]=value
    }
})
console.log(pro.name)//im is vivi
pro.name = 'bibi'//setting name=bibi
console.log(pro.name)//bibi
//apply
let target =function(){
    return 'Iam vivian'
}
let handler ={
    apply(target,ctx,args){
        console.log('to do apply')
        return Reflect.apply(...arguments);
    }
}
let pros = new Proxy(target,handler);
console.log(pros());//Iam vivian

 

你可能感兴趣的:(Es6)