zoukankan      html  css  js  c++  java
  • ES5扩展属性

    一:Object扩展属性

    1. Object.create(prototype, [descriptors])

      * 作用: 以指定对象为原型创建新的对象

      * 为新的对象指定新的属性, 并对属性进行描述

        value : 指定值

        writable : 标识当前属性值是否是可修改的, 默认为false

        configurable: 标识当前属性是否可以被删除 默认为false

        enumerable: 标识当前属性是否能用for in 枚举 默认为false

     

    2. Object.defineProperties(object, descriptors)

      * 作用: 为指定对象定义扩展多个属性

      * get :用来获取当前属性值得回调函数

      * set :修改当前属性值得触发的回调函数,并且实参即为修改后的值

      * 存取器属性:setter,getter一个用来存值,一个用来取值。

     var obj = {name : 'curry', age : 29}
        var obj1 = {};
        obj1 = Object.create(obj, {
            sex : {
                value : '男',//设置数值
                writable : true //权限能否修改
            }
        });
        obj1.sex = '女';
        console.log(obj1.sex);
    
        //Object.defineProperties(object, descriptors)
        var obj2 = {
            firstName : 'curry',
            lastName : 'stephen'
        };
        Object.defineProperties(obj2, {
            fullName : {
                get : function () {
                    return this.firstName + '-' + this.lastName
                },
                set : function (data) {
                    var names = data.split('-');
                    this.firstName = names[0];
                    this.lastName = names[1];
                }
            }
        });
        console.log(obj2.fullName);//获取扩展的属性自动调用get方法
        obj2.firstName = 'tim';
        obj2.lastName = 'duncan';
        console.log(obj2.fullName);
        obj2.fullName = 'kobe-bryant';//更改属性自动调用set方法
        console.log(obj2.fullName);
    

      

    对象本身的两个方法

        * get propertyName(){} 用来得到当前属性值的回调函数

        * set propertyName(){} 用来监视当前属性值变化的回调函数

    <script type='text/javascript'>
        var obj = {
            firstName : 'kobe',
            lastName : 'bryant',
            get fullName(){
                return this.firstName + ' ' + this.lastName
            },
            set fullName(data){
                var names = data.split(' ');
                this.firstName = names[0];
                this.lastName = names[1];
            }
        };
        console.log(obj.fullName);
        obj.fullName = 'curry stephen';
        console.log(obj.fullName);
    
    </script>
    

      

    二:数组的扩展属性

    1. Array.prototype.indexOf(value) : 得到值在数组中的第一个下标

    2. Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标

    3. Array.prototype.forEach(function(item, index){}) : 遍历数组

    4. Array.prototype.map(function(item, index){}) : 遍历数组返回一个新的数组,返回加工之后的值

    5. Array.prototype.filter(function(item, index){}) : 遍历过滤出一个新的子数组, 返回条件为true的值

    var arr = [1, 4, 6, 2, 5, 6];
        console.log(arr.indexOf(6));//2
        //Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标
        console.log(arr.lastIndexOf(6));//5
    
        //Array.prototype.forEach(function(item, index){}) : 遍历数组
        arr.forEach(function (item, index) {
            console.log(item, index);
        });
    
        //Array.prototype.map(function(item, index){}) : 遍历数组返回一个新的数组,返回加工之后的值
        var arr1 = arr.map(function (item, index) {
            return item + 10
        });
        console.log(arr, arr1);
    
        //Array.prototype.filter(function(item, index){}) : 遍历过滤出一个新的子数组, 返回条件为true的值
        var arr2 = arr.filter(function (item, index) {
            return item > 4
        });
        console.log(arr, arr2);
    

      三:函数this

    1. Function.prototype.bind(obj) :

      * 作用: 将函数内的this绑定为obj, 并将函数返回

    2. 面试题: 区别bind()与call()和apply()?

      * 都能指定函数中的this

      * call()/apply()是立即调用函数

      * bind()是将函数返回

    <script type="text/javascript">
        function fun(age) {
            this.name = 'kobe';
            this.age = age;
            console.log(this.age);
        }
       var obj = {};
        fun.apply(obj,[20]);
        fun.call(obj,30);
        fun.bind(obj,40)();
    
    
    
    </script>
    

      

  • 相关阅读:
    Java Socket通信实现私聊、群聊
    一套简单的web即时通讯——第二版
    一套简单的web即时通讯——第一版
    前后端API交互数据加密——AES与RSA混合加密完整实例
    跨境电商ERP中的自动化 3.平台订单自动发货
    跨境电商ERP中的自动化 2.平台商品和本地单品自动绑定
    跨境电商ERP中的自动化 1.平台订单自动同步至本地
    小特工具箱3.0版发布 春节优惠价99元/套
    河南农信移动支付解析
    win10 chrome 百分浏览器 centbrowser 收藏夹栏字体突然变小
  • 原文地址:https://www.cnblogs.com/love-life-insist/p/9909298.html
Copyright © 2011-2022 走看看