javascript之對象

來源:互聯網
上載者:User

標籤:

一、建立對象

  1、對象直接量。

    var point = { x:0,y:0 };  //point就是一個對象,跟C#不同,它不需要一定有類才能建立對象。

  2、通過new建立對象

    var d = new Date();    //建立一個Date對象

  3、原型

    Object.prototype  //用於擷取對象原型的引用。所有對象都直接或間接繼承自Object.prototype,相當於C#中的System.Object();

    通過new Date()建立的對象同時繼承自Date.prototype和Object.prototype。

  4、Object.create()

    Object.create()是一個靜態函數,可以用它建立一個新對象。

二、屬性的讀取和設定

  1、屬性的讀取有兩種方法。可以通過"."和"[]"來讀取。

    如:var author = book.author;

      var author = book["authoe"];

    設定也一樣:

      book.author = "劉德華";

      book["author"] = "劉德華";

    注意,如果對象的屬性名稱與保留字一樣,則必須通過方括弧讀取。

      如:book["class"];

     另外用方括弧訪問就個好處,就是在運行時確定屬性名稱如book["author" + 1];

  2、繼承

    如果要查詢對象o的屬性x,如果o沒有屬性x,則會到o的原型去查,一級一級往上,直到Object.prototype。

 
    var o = {};        o.x = 1;        var p = Object.create(o);        p.x = 2;         //改變了繼承的屬性x        document.write(p.x);  //輸出2,p.x繼承自o.x        var q = Object.create(o);        document.write(q.x);  //輸出1,p改變的是自己x,改變不了原型的x
 

    同時對原型鏈屬性的操作並不會真正更改原型鏈,只是在子物件中增加了一個同名屬性。更改是只是改了自己。

三、刪除屬性

    delete()   delete()方法只能夠刪除自有屬性,不能夠刪除繼承屬性。delete()只是斷開屬性和宿主對象的聯絡,而不回去操作屬性中的屬性。

        var book = {            author:"劉德華",            number:123        }        delete book.author;        //此行代碼也可寫成delete book["author"]        document.write(book.author);  //book.author的值為undefined,因為已經被刪除了
四、檢測屬性

    in:檢測某對象是否含有某個屬性

        var o = {            x:1        }          document.write("x" in o);  //返回true  x是o的一個屬性        document.write("y" in o);  //返回false  y不是o的屬性

    hasOwnProperty() 檢測對象中是否含有某屬性

        var o = {            x : 1        }        document.write(o.hasOwnProperty("x"));  //返回true  o中含有名為"x"的屬性        document.write(o.hasOwnProperty("y"));  //返回false  o中不含有名為"y"的屬性

    propertyIsEnumerable()  檢測到是自有屬性且這個屬性的可枚舉性,兩者都返回true它才返回true。繼承屬性返回false。

 
        var o = { x: 1,y:2,z:3 };        for (p in o) {            if (!o.hasOwnProperty(p)) {  //跳過繼承的屬性                continue;            }            if (typeof o[p] === "function") {  //跳過方法                continue;            }            document.write( p.toString() + "<br/>");        }
 

    get() set()屬性訪問器  沒有相應的get(),set()則為唯讀,或唯寫屬性。

 
        var o = {             x: 1,            get getx(){ return this.x; },            set setx(n){this.x = n;}         };         document.write(o.getx);  //讀取屬性值,返回1         o.setx = 2;         //如果將set setx(n)這行注釋,則這行代碼可以相當於不存在,設定無效。x依然為1,但不會報錯。         document.write(o.getx);  //再次讀取返回2
 五、屬性的特性

    writeable:是否可寫。

    enumerable:是否可枚舉。

    configurable:是否可配置。

六、對象的序列化

  這個東西非常有用,能夠將對象序列化成字串之後傳輸,也能夠將對象以字串的格式存在cookie裡,拿出來後再還原序列化成對象。

    JSON.stringify()   序列化對象(對象轉成字串便於傳輸)

    JSON.parse()    還原序列化對象(字串轉換成原對象,是來源物件深拷貝)

            var man = { name: "張三", Age: 24 };            var str = JSON.stringify(man);    //將對象man序列化成JSON字串            document.write(str);            var newman = JSON.parse(str);     //將字串反序列成對象 document.write("<br/>" + newman.name + "," + newman.Age);  //輸出還原序列化後的對象的屬性

javascript之對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.