最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alternative to multiple Object.defineProperty - Stack Overflow

programmeradmin5浏览0评论

Given a product might have several attributes such as name, price, sku, description and so on - the following will bee quite long winded to describe a product model...

function Product(data) {

    var productData = data || {};

    Object.defineProperty(this, "sku", {
        get: function() {
            return productData.sku;
        }
    });

    Object.defineProperty(this, "name", {
        get: function() {
            return productData.name;
        }
    });

    Object.defineProperty(this, "price", {
        get: function() {
            return productData.price;
        }
    });
}

module.exports = Product;

What alternatives are there in javascript for this and how is this normally handled?

Given a product might have several attributes such as name, price, sku, description and so on - the following will bee quite long winded to describe a product model...

function Product(data) {

    var productData = data || {};

    Object.defineProperty(this, "sku", {
        get: function() {
            return productData.sku;
        }
    });

    Object.defineProperty(this, "name", {
        get: function() {
            return productData.name;
        }
    });

    Object.defineProperty(this, "price", {
        get: function() {
            return productData.price;
        }
    });
}

module.exports = Product;

What alternatives are there in javascript for this and how is this normally handled?

Share Improve this question asked Feb 15, 2015 at 19:24 Marty WallaceMarty Wallace 35.9k57 gold badges143 silver badges209 bronze badges 4
  • 5 How about Object.defineProperties()? – Pointy Commented Feb 15, 2015 at 19:26
  • 1 You can use a loop to define each property, instead of writing the same function multiple times. – levi Commented Feb 15, 2015 at 20:13
  • Why do you use Object.defineProperty and setters at all? – Bergi Commented Feb 15, 2015 at 21:58
  • I want them read only – Marty Wallace Commented Feb 15, 2015 at 22:12
Add a ment  | 

2 Answers 2

Reset to default 6

@Pointy deserves the points here with Object.defineProperties-

function Product(data) {

    var productData = data || {};

    Object.defineProperties(this, {
      "sku": {
          get: function() {
            return productData.sku;
          }
       }, 
       "name": {
           get: function() {
             return productData.name;
           }
       },
       "price": {
           get: function() {
             return productData.price;
           }
        }
    });
}

module.exports = Product;

Support is near identical to Object.defineProperty so there is no real reason not to use this method when defining multiple properties at the same time.

You can use a single loop to define all the properties:

var self = this;
Object.keys(productData).forEach(function(prop){
    Object.defineProperty(self, prop, {
        get: function() {
            return productData[prop];
        }
    });
});

Demo

发布评论

评论列表(0)

  1. 暂无评论