天天看點

es6 Object 資料屬性和通路器屬性

總結

ES5提供了Object.getOwnPropertyDescripter()方法來擷取給定屬性的描述符。      

ps:getOwnPropertyDescripter

該方法接收兩個參數:屬性所在的對象和要讀取其描述符的屬性名稱。結果會傳回一個對象,如果是通路器屬性,傳回的對象有configuable、enumerable、get和set;如果是資料屬性,這個傳回對象的屬性包括configuable、enumerable、writable和value。對于上面的例如,使用如下:

===============================

電梯: 原文

Object.create() 

用 

Object.create

實作類式繼承

// Shape - 父類(superclass)
function Shape() {
  this.x = 0;
  this.y = 0;
}

// 父類的方法
Shape.prototype.move = function(x, y) {
  this.x += x;
  this.y += y;
  console.info('Shape moved.');
};

// Rectangle - 子類(subclass)
function Rectangle() {
  Shape.call(this); // call super constructor.
}

// 子類續承父類
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

// 因為使用“.prototype =...”後,constructor會改變為“=...”的那個
// constructor,是以要重新指定.constructor 為自身。