zoukankan      html  css  js  c++  java
  • ES6-2

    {
    // Symbol数据类型,声明一个独一无二的数据
    let a1 = Symbol();
    let a2 = Symbol();
    console.log(a1 === a2)
    let a3 = Symbol.for('a3');
    let a4 = Symbol.for('a3');
    console.log(a3 === a4)

    }
    {
    let a1 = Symbol.for('abc');
    let obj = {
    [a1]: '123',
    'abc': 345,
    'c': 456
    }
    console.log(obj)
    for (let [key ,value] of Object.entries(obj)) {
    console.log(key, value)//通过for遍历找不到Symbol的属性值
    }
    // 取Symbol的属性值
    Object.getOwnPropertySymbols(obj).forEach((item)=>{
    console.log(obj[item])//只取到Symbol的值
    })
    Reflect.ownKeys(obj).forEach((item)=>{
    console.log(item,obj[item])
    })
    }
    /*---- Set Map ----*/
    {
    let list = new Set();
    list.add(5);
    list.add(7);
    console.log(list.size)//获取Set中的长度,类似length
    }
    {
    let arr = [1,2,3,4,5]
    let list = new Set(arr);
    console.log(list)
    }
    {
    let list = new Set();
    list.add(1);
    list.add(2)
    list.add(1)
    console.log(list)//重复的数据不会被添加进去
    let arr = [1,2,3,41,1,2,3,'2']
    let list2 = new Set(arr);//对数组里面的数据不会做类型转换
    console.log(list2)
    }
    {
    let arr = ['add','delete','clear','has'];
    let list = new Set(arr)
    console.log(list.has('add'))//delete clear add 类似用法
    }
    {
    let arr = ['add','delete','clear','has'];
    let list = new Set(arr)
    for (let key of list.keys()) {
    console.log(key)
    }
    for (let [key,value] of list.entries()) {
    console.log(key, value)
    }
    }

    {
    let weakList = new WeakSet();
    //weakSet和Set支持的数据类型不同,weakSet只能是对象并且不会去检测是不是被垃圾回收
    let arg = {}
    let a = 2
    weakList.add(arg)
    //weakList.add(a)//报错
    }
    /*---Map---------*/
    {
    let map = new Map();
    let arr = ['123']
    map.set(arr,345)//set内参数第一个是key值
    console.log(map,map.get(arr))
    }
    {
    let map = new Map([['a',123],['b',456]])
    console.log(map)
    console.log(map.size)
    console.log(map.delete('a'))
    // clear
    }
    {
    // 数据结构横向对比,增删改查
    let map = new Map()
    let array = []
    // 增
    map.set('t',1)
    array.push({t:1})
    console.log(map, array)
    // 查
    let map_exist = map.has('t');
    let array_exit = array.find(item=>item.t)
    console.log(map_exist, array_exit)
    // 改
    map.set('t',2)
    console.log(map)
    // 删
    map.delete('t')
    console.log(map)
    let index = array.findIndex(item => item.t)
    array.splice(index,1)
    }
    {
    // set和array的对比
    let set = new Set()
    let array = []
    // 增
    set.add({t: 1})
    array.push({t: 1})
    // 查
    let set_exit = set.has({t:1})
    // 改
    set.forEach(item=>item.t?item.t=2:'')
    console.log(set)
    // 删
    set.forEach(item=>item.t?set.delete(item):'')
    console.log(set)
    }
    {
    let item = {t: 1};
    let map = new Map();
    let set = new Set();
    let obj = {
    t: 1
    };
    console.log('t' in obj)
    delete obj['t']
    console.log(obj)
    }
    // Proxy
    {
    let obj = {
    time: '20170904',
    name: 'net',
    _r: 123
    }
    let monitor = new Proxy(obj,{
    // 代理对象属性的读取,不让用户直接操作obj
    get(target,key){
    return target[key].replace('2017', '2018')//把所有属性值中的2017替换成2018
    },
    set(target, key ,value) {
    if(key === 'name') {
    return target[key] = value
    } else {
    return target[key ]
    }
    },
    // 拦截key in Object操作
    has(target, key) {
    if (key === 'name') {
    return target[key]
    } else {
    return false
    }
    },
    // 拦截delete
    deleteProperty(target,key) {
    if (key.indexOf('_' ) > -1) {
    delete target[key]
    } else {
    return target[key]
    }
    },
    // 拦截Object.keys等
    ownKeys(target) {
    return Object.keys(target).filter(item=>item!='time')
    }
    })//代理,get和set控制读写
    // console.log(monitor.time)
    // console.log('name' in monitor,'time' in monitor)
    // delete monitor.time
    // console.log(monitor)
    // delete monitor._r
    // console.log(monitor)
    console.log(Object.keys(monitor))
    }

    {
    let obj = {
    time:'20170904',
    name:'net',
    _r:123
    }
    console.log(Reflect.get(obj,'time'))//profxy有的方法,Reflect都有
    Reflect.set(obj,'name','hello')
    console.log(obj)
    }
    // 数据校验
    {
    function validator(target,validator) {
    return new Proxy(target,{
    _validator: validator,
    set(target,key,value,proxy) {
    if (target.hasOwnProperty(key)) {
    let va = this._validator[key];
    if (!!va(value)) {
    return Reflect.set(target,key,value,proxy)
    } else {
    throw Error(`不能设置${key}到${value}`);
    }
    } else {
    throw Error(`${key}不存在`)
    }
    }
    })
    }
    const personCalidators={
    name(val) {
    return typeof val === "string"
    },
    age(val) {
    return typeof val === 'number' && val > 18
    }
    }
    class Person {
    constructor(name, age) {
    this.name = name;
    this.age = age;
    return validator(this,personCalidators)
    }
    }
    // const person = new Person('lilei',90)
    // person.name = 323
    // console.info(person)
    }

    {
    // 基本定义和生成实例
    class Parent{
    constructor(name='hello world'){
    this.name=name;
    }
    }
    let v_parent=new Parent('v');
    console.log('构造函数和实例',v_parent);
    }
    {
    // 继承
    class Parent{
    constructor(name='hello world'){
    this.name=name;
    }
    }

    class Child extends Parent{

    }

    console.log('继承',new Child());
    }
    {
    // 继承传递参数
    class Parent{
    constructor(name='hello world'){
    this.name=name;
    }
    }

    class Child extends Parent{
    constructor(name='child'){
    super(name);//子类向父类传递参数
    this.type='child';//子类增加自己的属性,要放在super之后
    }
    }

    console.log('继承传递参数',new Child('hello'));
    }

    {
    // getter,setter
    class Parent{
    constructor(name='hello world'){
    this.name=name;
    }

    get longName(){
    return 'he'+this.name
    }

    set longName(value){
    this.name=value;
    }
    }

    let v=new Parent();
    console.log('getter',v.longName);
    v.longName='hello';
    console.log('setter',v.longName);
    }
    {
    // 静态方法, 是通过类去调用,而不是类的实例去调用
    class Parent{
    constructor(name='mukewang'){
    this.name=name;
    }

    static tell(){
    console.log('tell');
    }
    }

    Parent.tell();

    }
    // Promise
    {
    // 基本定义
    let ajax=function(callback){
    console.log('执行');
    setTimeout(function () {
    callback&&callback.call()
    }, 1000);
    };
    ajax(function(){
    console.log('timeout1');
    })
    }

    {
    let ajax=function(){
    console.log('执行2');
    return new Promise(function(resolve,reject){
    setTimeout(function () {
    resolve()
    }, 1000);
    })
    };

    ajax().then(function(){
    console.log('promise','timeout2');
    })
    }

    {
    let ajax=function(){
    console.log('执行3');
    return new Promise(function(resolve,reject){
    setTimeout(function () {
    resolve()
    }, 1000);
    })
    };

    ajax()
    .then(function(){
    return new Promise(function(resolve,reject){
    setTimeout(function () {
    resolve()
    }, 2000);
    });
    })
    .then(function(){
    console.log('timeout3');
    })
    }

    {
    let ajax=function(num){
    console.log('执行4');
    return new Promise(function(resolve,reject){
    if(num>5){
    resolve()
    }else{
    throw new Error('出错了')
    }
    })
    }

    ajax(6).then(function(){
    console.log('log',6);
    }).catch(function(err){
    console.log('catch',err);
    });

    ajax(3).then(function(){
    console.log('log',3);
    }).catch(function(err){
    console.log('catch',err);
    });
    }
  • 相关阅读:
    sass08 if while for each
    sass07 函数
    sass06 mixin
    sass05 数据类型,数据运算
    sass04 嵌套、继承、占位符
    批量导出docker images 的一个简单方法
    ARM 版本 瀚高 数据库的启动命令
    PHPStorm+Wamp+Xdebug+Windows7调试代码
    在Windows Server 2012 中安装 .NET 3.5 Framework
    Windows Server 2012 GUI与Core的切换
  • 原文地址:https://www.cnblogs.com/0828-li/p/9960166.html
Copyright © 2011-2022 走看看