ES5擴展屬性

一:Object擴展屬性javascript

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

  * 做用: 以指定對象爲原型建立新的對象java

  * 爲新的對象指定新的屬性, 並對屬性進行描述面試

    value : 指定值數組

    writable : 標識當前屬性值是不是可修改的, 默認爲falseapp

    configurable: 標識當前屬性是否能夠被刪除 默認爲false函數

    enumerable: 標識當前屬性是否能用for in 枚舉 默認爲falsethis

 

2. Object.defineProperties(object, descriptors)prototype

  * 做用: 爲指定對象定義擴展多個屬性htm

  * 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>
相關文章
相關標籤/搜索