js之oop <二> 對象屬性

來源:互聯網
上載者:User

標籤:

js中對象屬性可以動態添加和刪除。刪除對象屬性用delete關鍵字。

    function obj(){  }    var oo = new obj();    oo.a = "a";    oo.b = "b";    oo.a; //輸出a    oo.b; //輸出b    delete oo.a;    oo.a; //輸出undefined

在js中對象的屬性以索引值對的方式來體現,且(在遍曆時)無序。js中屬性名稱(也就是key)可以用點來訪問(或賦值),也可以用中括弧key值來訪問(或賦值)(用中括弧key訪問對象屬性時,key必須是string類型)

var a = { x:5,y:2 }; a.x; //輸出5 a[‘x‘];//輸出5

對象屬性的遍曆,遍曆對象屬性常用有兩種方法。

var obj = { a1:1, a2:2, a3:3, a4:4, a5:5 };for(var i=1;i<=5;i++ ){    console.log(obj[‘a‘ + i]);}//輸出 1 2 3 4 5
var obj = { a:1, b:2, c:3, d:4, e:5 };for(var item in obj){    console.log(obj[item]);}//輸出 1 2 3 4 5

這裡需要注意,for in 遍曆對象屬性是無序的,有可能遍曆到原型鏈上的屬性。且使用 for in 遍曆時只能使用[]讀取屬性,不能使用.讀取。

function obj_pro(){}    //建立obj_pro構造器obj_pro.prototype.f = 55;   //建立obj_pro原型屬性fvar obj = new obj_pro();    //obj_pro建立obj對象//obj對象屬性迴圈賦值for(var i=1;i<=5;i++){    obj[‘a‘ + i] = (i+9);}//遍曆obj對象屬性for(var item in obj){    console.log(obj[item]);}//輸出 10 11 12 13 14 55 這裡遍曆把原型鏈上的屬性f遍曆出來了

****************************************************************************************************************

屬性的 get/set

get/set 和其他語言一樣,用來封裝對象屬性。get設定讀取,set設定寫入。

var person = {    name:"Mirage",    get age(){        return age;    },    set age(val){        if(val < 0 || isNaN(val) || val > 150){            age = 18;        }else{            age = val;        }    }}person.age = -99;console.log(person.age);    //輸出 18

age的值可以在set中做判斷,同樣也可以在get中做判斷。

var person = {    name:"Mirage",    get age(){        if(age < 0 || isNaN(age) || age > 150){            return 18;        }else{            return age        }    },    set age(val){        age = val;    }}person.age = -99;console.log(person.age);   //輸出 18

可以通過get/set對特定屬性設定唯讀或不可讀

var player = {    get gameYear(){        return 5;    }}player.gameYear = 59;console.log(player.gameYear);   //輸出5

僅設定get傳回值,該屬性就不可修改(唯讀)。

var player = {    set age(val){        if(val < 0 || isNaN(val) || val > 150){            age = 18;        }else{            age = val;        }    }}player.age = "age";      //player.pName值為18console.log(player.age);  //輸出 undefined

僅設定set,該屬性可修改,但不可讀取。

*****************************************************************************************************************

原型鏈的get/set

對構造器建立的對象設定get/set屬性需要用到 Object.defineProperty 。
Object.defineProPerty函數可以添加對象屬性,並修改現有屬性的特性。
Object.definePerperty參數:
  object(必):目標對象(也可為對象原型添加,還可為DOM對象)。type:ObjectName(對象名)。
  propertyname(必):屬性名稱。type:string(字串)。
  descriptor(必):屬性描述器。type:Object(對象_字面量)。

function person(){}Object.defineProperty(person.prototype,                      "age",                      {                        get:function(){                            return age;                        },                        set:function(val){                            if(val < 0 || isNaN(val) || val > 150){                                age = 18;                            }else{                                age = val;                            }                        }                      });var p1 = new person();p1.age = 6150;console.log(p1.age);    //輸出 18。

通過defineProperty可以對對象原型屬性進行修改。

 

js之oop <二> 對象屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.