ES 语法

作者: 小凡凡520 | 来源:发表于2019-08-06 09:05 被阅读0次
    一、常量
        // ES5
        Object.defineProperty(window,"PI2",{
            value:123,
            writable:false,
        }); 
        console.log(window.PI2);
        window.PI2 = 333;
        console.log(window.PI2);
    
        // ES6
        const PI3 = "test";
        console.log(PI3);
    
    二、作用域
        // ES5
        ((function () {
            const  foo = function () {
                return 2
            };
            console.log("foo()==2",foo() === 2);
    
            ((function () {
                const  foo = function () {
                    return 1
                };
                console.log("foo()==1",foo() === 1);
            })());
        })());
    
        // ES6
        {
            const  foo = function () {
                return 3
            };
            console.log("foo()==3",foo() === 3);
            {
                const  foo = function () {
                    return 4
                };
                console.log("foo()==4",foo() === 4);
            }
        }
    
    三、箭头函数

    this 为定义时的对象

        var events = [1,2,3,4,5];
        var odds = events.map(function (value) {
            return value * 2;
        });
        console.log(odds);
    
        var results = events.map(v => v + 1);
        console.log(results);
    
    
        {
            var  person = function () {
                this.name = "chenfan";
                this.age = 29;
                this.a = {
                    name:"WHB",
                    b:function () {
                        return this.name
                    }
                }
            }
    
            console.log(new person().a.b());
        }
    
        {
            var  person = function () {
                this.name = "chenfan";
                this.age = 29;
                this.a = {
                    name:"WHB",
                    b: () => {
                        return this.name
                    }
                }
            }
    
            console.log(new person().a.b());
        }
    
    四、默认参数
        {
            function add(x, y) {
                if (x === undefined) {
                    x = 1;
                }
                if (y === undefined) {
                    y=2;
                }
                return x + y;
            }
    
            console.log(add(100));
        }
    
        {
            function add(x, y = 100) {
                return x + y;
            }
    
            console.log(add(100));
        }
    
    
        {
            function check() {
                throw new Error('fail');
            }
    
            function add(x = check()) {
    
            }
    
            add();
        }
    
        {
            function add() {
                var a = Array.prototype.slice.call(arguments);
                var sum = 0;
                a.forEach(function (item) {
                    sum = item + 1;
                });
                return sum
            }
            console.log(add(1,2));
        }
    
        {
            function add(...a) {
                var sum = 0;
                a.forEach(item => {
                    sum += item
                });
                return sum;
            }
    
            console.log(add(1,2));
        }
    
        {
            var arr1 = [1,2,3];
            var arr2 = [4,5,6];
            // ES5 连接
            var result = arr1.concat(arr2);
            console.log(result);
        }
    
        {
            var arr1 = [4,5,6];
            var arr2 = [
                1,2,3,...arr1
            ]
            console.log(arr2);
        }
    
    五、数据保护
        {
            var person = function () {
                var data ={
                    name:"chenfan",
                    age:29
                };
                this.get = function (key) {
                    return data[key];
                };
                this.set = function (key,value) {
                    data[key] = value;
                }
            };
    
            var p = new person();
            p.set('name','11111');
    
            console.log(p.get('name'));
        }
    
    
    
        {
            var person = {
    
            };
    
            Object.defineProperty(person,'name',{
                value:"456",
                writable:false
            });
    
            person.name = "123";
            console.log(person.name);
        }
    
    六、对象代理
        {
            let Person = {
                name:"888"
            };
    
            let person = new Proxy(Person,{
                get(target, key) {
                    return target[key]
                },
                set(target,key, value) {
                    if (key != "name") {
                        target[key] = value
                    }
                }
            })
    
            console.log(person.name);
            person['name'] = 'wwww';
            console.log(person.name);
        }

    相关文章

      网友评论

          本文标题:ES 语法

          本文链接:https://www.haomeiwen.com/subject/lnhmdctx.html