js高級程序設(shè)計(jì)筆記2(面向?qū)ο螅?/h2>

一、理解對象

1. 數(shù)據(jù)屬性

數(shù)據(jù)屬性包含一個(gè)數(shù)據(jù)值的位置。在這個(gè)位置可以讀取和寫入值。數(shù)據(jù)屬性有 4 個(gè)描述其行為的特性。

  • [[Configurable]]:表示能否通過 delete 刪除屬性從而重新定義屬性,能否修改屬性的特性,或者能否把屬性修改為訪問器屬性。像前面例子中那樣直接在對象上定義的屬性,它們的這個(gè)特性默認(rèn)值為 true。
  • [[Enumerable]]:表示能否通過 for-in 循環(huán)返回屬性。像前面例子中那樣直接在對象上定義的屬性,它們的這個(gè)特性默認(rèn)值為 true。
  • [[Writable]]:表示能否修改屬性的值。像前面例子中那樣直接在對象上定義的屬性,它們的這個(gè)特性默認(rèn)值為 true。
  • [[Value]]:包含這個(gè)屬性的數(shù)據(jù)值。讀取屬性值的時(shí)候,從這個(gè)位置讀;寫入屬性值的時(shí)候,把新值保存在這個(gè)位置。這個(gè)特性的默認(rèn)值為 undefined。
var person = {};
Object.defineProperty(person, "name",
 {writable: false,value: "Nicholas"});
alert(person.name); //"Nicholas"
person.name = "Greg";
alert(person.name); //"Nicholas"  ```
注意:把 configurable 設(shè)置為 false,表示不能從對象中刪除屬性。如果對這個(gè)屬性調(diào)用 delete,則在非嚴(yán)格模式下什么也不會(huì)發(fā)生,而在嚴(yán)格模式下會(huì)導(dǎo)致錯(cuò)誤。而且,一旦把屬性定義為不可配置的,就不能再把它變回可配置了。此時(shí),再調(diào)用 Object.defineProperty()方法修改除 writable 之外的特性,都會(huì)導(dǎo)致錯(cuò)誤 

##2、訪問器屬性 
-  [[Configurable]]:表示能否通過 delete 刪除屬性從而重新定義屬性,能否修改屬性的特性,或者能否把屬性修改為數(shù)據(jù)屬性。對于直接在對象上定義的屬性,這個(gè)特性的默認(rèn)值為true。
-  [[Enumerable]]:表示能否通過 for-in 循環(huán)返回屬性。對于直接在對象上定義的屬性,這個(gè)特性的默認(rèn)值為 true。
-  [[Get]]:在讀取屬性時(shí)調(diào)用的函數(shù)。默認(rèn)值為 undefined。? [[Set]]:在寫入屬性時(shí)調(diào)用的函數(shù)。默認(rèn)值為 undefined。 
```
//訪問器屬性不能直接定義,必須使用 Object.defineProperty()來定義。
var book = {_year: 2004,edition: 1};
Object.defineProperty(book, "year", {
        get: function(){return this._year;},
        set: function(newValue){
            if (newValue > 2004) {
                    this._year = newValue;this.edition += newValue - 2004;
            }
        }
});
book.year = 2005;
alert(book.edition); //2 ```
######注意:不一定非要同時(shí)指定 getter 和 setter。只指定 getter 意味著屬性是不能寫,嘗試寫入屬性會(huì)被忽略。在嚴(yán)格模式下,嘗試寫入只指定了 getter 函數(shù)的屬性會(huì)拋出錯(cuò)誤。類似地,只指定 setter 函數(shù)的屬性也不能讀,否則在非嚴(yán)格模式下會(huì)返回 undefined,而在嚴(yán)格模式下會(huì)拋出錯(cuò)誤。 

#二、原型
```
//組合使用構(gòu)造函數(shù)模式和原型模式 
function Person(name, age, job){
        this.name = name;
        this.age = age;
        this.job = job;
        this.friends = ["Shelby", "Court"];
}
Person.prototype = {
        constructor : Person,
        sayName : function(){
            alert(this.name);
        }
}
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27,"Doctor");
person1.friends.push("Van");
alert(person1.friends); //"Shelby,Count,Van"
alert(person2.friends); //"Shelby,Count"
alert(person1.friends === person2.friends); //false
alert(person1.sayName === person2.sayName); //true ```

#三、繼承
###1、原型鏈 
```
function SuperType(){
    this.property = true;
} 
SuperType.prototype.getSuperValue = function(){
    return this.property;
};
function SubType(){
    this.subproperty = false;
}
//繼承了 SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function (){
    return this.subproperty;
};
var instance = new SubType();
alert(instance.getSuperValue()); //true ```


![構(gòu)造函數(shù)和原型之間的關(guān)系 ](http://upload-images.jianshu.io/upload_images/2572649-18f9ce923ba6c63d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

```
//繼承了 SuperType
SubType.prototype = new SuperType();
//使用字面量添加新方法,會(huì)導(dǎo)致上一行代碼無效
SubType.prototype = {
    getSubValue : function (){
    return this.subproperty;
    },
    someOtherMethod : function (){
        return false;
    }
}; ```

###2、借用構(gòu)造函數(shù) (很少單獨(dú)使用)
```
function SuperType(){
    this.colors = ["red", "blue", "green"];
}
function SubType(){
    //繼承了 SuperType
    SuperType.call(this);//或者:SuperType.apply(this)
}
var instance1 = new SubType();
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
var instance2 = new SubType();
alert(instance2.colors); //"red,blue,green" ```
###3、寄生組合式繼承 
```
function SuperType(name){
    this.name = name;
    this.colors = ["red", "blue", "green"];
}
SuperType.prototype.sayName = function(){
    alert(this.name);
};
function SubType(name, age){
    SuperType.call(this, name); //第二次調(diào)用 SuperType()
    this.age = age;
}
SubType.prototype = new SuperType(); //第一次調(diào)用 
SuperType()SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
    alert(this.age);
} ```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容