JS和JQ的總結:對象

來源:互聯網
上載者:User

標籤:null   array   修改   key   屬性   類型   delete   conf   var   

1、對象的基本操作  create  set  query delete test  enumerate

2、對象的屬性特性  可寫  可枚舉  可配置(是否可刪除)

3、對象的對象特性 

對象的原型 prototype

對象的類  標示物件類型的字串

對象的擴充標記  是否可添加新屬性

 //建立對象的三種方法

 4、對象直接量

 var empty = {};

 var point = {x:0,y:0};

5、new建立對象

 var o = new Object();  //Null 物件  和{}一樣

 var a = new Array();

 6、Object.create()

 var o1 = Object.create({x:1,y:2});  //o1 繼承自對象原型{x:1,y:2}

 var o2 = Object.create(null);       //o2不繼承任何屬性和方法

 var o3 = Object.create(Object.prototype); 

 //Null 物件和 {} new Object()一樣

7、屬性的操作  作為關聯陣列的對象

 object.property

 object["property"]   散列  映射  字典 關聯陣列

8、繼承

 js的繼承只能從父類擷取屬性的值,而不能修改原型鏈

 如果對象book為null或undefined

 book.subtitle.length會報錯

  解決方案

 var len = book && book.subtitle && book.subtitle.length;

 刪除屬性

  delete book.subtitle

檢測屬性  in  hasOwnPreperty()  propertyIsEnumerable()

 var o = {x:1};

 "x" in o

  "toString" in

 hasOwnPreperty();判斷屬性是否是自己的。繼承屬性返回false

propertyIsEnumerable() 只有是自有屬性,並可枚舉

擷取所有屬性

  Object.keys()

 Object.getOwnPropertyNames()

9、屬性getter和setter

 var o = {

 x: 0,

 y: 1,

 set r(value) { value = this.r;},

 get r() { return this.x + this.y;}

 };

 x資料屬性,r存取器屬性

10、屬性的特性  ecmascript5   老的ie不支援如下用法

 資料屬性的特性:值value 可寫性writable

可枚舉enumerable

 可配置configurable

存取器屬性特性:get  set

 可枚舉enumerable

 可配置configurable

  //返回{value:1,writable:true,emunerable:true,configurable:true}

  Object.getOwnPropertyDescriptor({x:1},x)

  //查不到屬性 返回undefined

 設定屬性的特性  不能修改繼承的屬性特性

  Object.defineProperty()

 //設定不可枚舉屬性

 var o = {};

 Object.defineProperty(o,"x",{

 value:1,

writable:true,

emunerable:false,

configurable:true

 })

 //設定唯讀

 Object.defineProperty(o,"x",{writable:false})

 修改多個屬性特性

 Object.defineProperties()

擴充Object.prototype   書 P137

 11、對象的三個屬性

 prototype  class  extensible attribute

 查詢對象的原型

 ecmascript5中 Object.getPrototypeOf(o1)

 ecmascript3中 o1.constructor.prototype

通過對象直接量或new Object()方式建立的對象

 包含一個constructor的屬性,指Object()的建構函式

 constructor.prototype才是真正的原型

p.isPrototypeOf(o)檢測對象p是否是o的原型

 //擷取對象的類型 字串

 Object.prototype.toString.call(o1).slice(8,-1)

 12、對象的可擴充性

 對象是否可以新加屬性

 Object.preventExtensions() 設定對象不可擴充

 Object.isExtensible()

 Object.seal()  除了將對象設定為不可擴充,還將屬性設定為不可配置

 Object.freeze()

 除了將對象設定為不可擴充,將屬性設定為不可配置,還把屬性設定為唯讀

 

JS和JQ的總結:對象

聯繫我們

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