JavaScript學習心得06

來源:互聯網
上載者:User

標籤:不可   rip   continue   目標   TE   改變   版本   通過   set   

Object()
  1. 如果參數是各種原始類型的值,轉換成對象就是原始類型值對應的封裝對象:
var obj = Object(1);obj instanceof Object // trueobj instanceof Number // truevar obj = Object(‘foo’);obj instanceof Object // trueobj instanceof String // truevar obj = Object(true);obj instanceof Object // trueobj instanceof Boolean // true
  1. 如果參數是一個對象,那麼返回自身,不做任何改變:
//利用這一點,可以判斷一個變數是否為對象function isObject(value) {  return value === Object(value);}isObject([]) // trueisObject(true) // false
  • 對於一般的對象來說,Object.keys()Object.getOwnPropertyNames()返回的結果是一樣的。只有涉及不可枚舉屬性時,才會有不一樣的結果。Object.keys方法只返回可枚舉的屬性Object.getOwnPropertyNames方法還返回不可枚舉的屬性名稱
如何計算對象屬性的個數?
var obj = {  p1: 123,  p2: 456};Object.keys(obj).length // 2
Object之靜態方法(本身的方法)分類
  1. 對象屬性模型的相關方法;
  2. 控制對象狀態的方法;
  3. 原型鏈方法。
Object.prototype.toString可以看出一個值到底是什麼類型
Object.prototype.toString.call(2) // “[object Number]”Object.prototype.toString.call(‘’) // “[object String]”Object.prototype.toString.call(true) // “[object Boolean]”Object.prototype.toString.call(undefined) // “[object Undefined]”Object.prototype.toString.call(null) // “[object Null]”Object.prototype.toString.call(Math) // “[object Math]”Object.prototype.toString.call({}) // “[object Object]”Object.prototype.toString.call([]) // “[object Array]”
屬性描述對象attributes object
  • 有六個元屬性,每一個對象的每一個屬性都擁有一個屬性描述對象:
  1. value:屬性值;
  2. writable:是否可寫?
  3. enumerable:是否可遍曆?
  4. configurable:是否可配置該屬性描述對象?
  5. get:每次讀取該屬性,都會調用這個存取子。
  6. set
  • 注意,Object.getOwnPropertyDescriptor方法只能用於對象自身的屬性,不能用於繼承的屬性。

  • 一旦定義了存取子get(或存值函數set),就不能將writable屬性設為true,或者同時定義value屬性,否則會報錯。

  • 如果對象的 JSON 格式輸出要排除某些屬性,就可以把這些屬性的enumerable設為false。

configurable的愛恨情仇
  1. configurable為false時,valuewritableenumerable
    configurable都不能被修改了;
  2. configurable為false時, writable在false改為true會報錯,true改為false是允許的;
  3. 只要 configurablewritable有一個是true,就可以通過Object.defineProperty方法來改value
  4. 但是當 configurable為false時,不能直接通過目標屬性賦值來修改value
  5. configurable設為false時,該屬性不能被刪除;
如何寫 setter還有 getter呢?

有兩種寫法:

  1. 傳統的利用Object.defineProperty方法來修改setget
var obj = Object.defineProperty({}, ‘p’, {  get: function () {    return ‘getter’;  },  set: function (value) {    console.log(‘setter: ‘ + value);  }});obj.p // “getter”obj.p = 123 // “setter: 123”
  1. 簡化版本,應用更加廣泛:
var obj = {  get p() {    return ‘getter’;  },  set p(value) {    console.log(‘setter: ‘ + value);  }};
如何完美拷貝對象?(連原對象的屬性描述對象都不放過~)
var extend = function (to, from) {  for (var property in from) {//過濾掉繼承的屬性,因為getOwnPropertyDescriptor讀不出繼承屬性    if (!from.hasOwnProperty(property)) continue;    Object.defineProperty(      to,      property,      Object.getOwnPropertyDescriptor(from, property)    );  }  return to;}extend({}, { get a(){ return 1 } })// { get a(){ return 1 } })

JavaScript學習心得06

相關文章

聯繫我們

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