how to use javascript Object.defineProperty?

本文详细介绍了在JavaScript中如何使用Object.defineProperty方法将字段转换为属性,包括getter和setter的使用,以及如何通过配置可配置性、可写性和枚举性来控制属性的行为。此外,文章还讨论了在大型项目中维护代码质量和避免重构风险的重要性。

Since you asked a similar question, let's take it step by step. It's a bit longer, but it may save you much more time than I have spent on writing this:

Property is an OOP feature designed for clean separation of client code. For example, in some e-shop you might have objects like this:

function Product(name,price) {
  this.name = name;
  this.price = price;
  this.discount = 0;
}

var sneakers = new Product("Sneakers",20); // {name:"Sneakers",price:20,discount:0}
var tshirt = new Product("T-shirt",10);  // {name:"T-shirt",price:10,discount:0}

Then in your client code (the e-shop), you can add discounts to your products:

function badProduct(obj) { obj.discount+= 20; ... }
function generalDiscount(obj) { obj.discount+= 10; ... }
function distributorDiscount(obj) { obj.discount+= 15; ... }

Later, the e-shop owner might realize that the discount can't be greater than say 80%. Now you need to find EVERY place in the client code and add a line

if(obj.discount>80) obj.discount = 80;

Then the e-shop owner may further change his strategy, like "if the customer is reseller, the maximal discount can be 90%". And you need to do the change on multiple places again plus you need to remember to alter these lines whenever the strategy is changed. This is a bad design. That's why encapsulation is the basic principle of OOP. If the constructor was like this:

function Product(name,price) {
  var _name=name, _price=price, _discount=0;
  this.getName = function() { return _name; }
  this.setName = function(value) { _name = value; }
  this.getPrice = function() { return _price; }
  this.setPrice = function(value) { _price = value; }
  this.getDiscount = function() { return _discount; }
  this.setDiscount = function(value) { _discount = value; } 
}

Then you can just alter the getDiscount (accessor) and setDiscount (mutator) methods. The problem is that most of the members behave like common variables, just the discount needs special care here. But good design requires encapsulation of every data member to keep the code extensible. So you need to add lots of code that does nothing. This is also a bad design, a boilerplate antipattern. Sometimes you can't just refactor the fields to methods later (the eshop code may grow large or some third-party code may depend on the old version), so the boilerplate is lesser evil here. But still, it is evil. That's why properties were introduced into many languages. You could keep the original code, just transform the discount member into a property with get and set blocks:

function Product(name,price) {
  this.name = name;
  this.price = price;
  var _discount; // private member
  Object.defineProperty(this,"discount",{  // this answers the comment (pun intended)
    get: function() { return _discount; },
    set: function(value) { _discount = value; if(_discount>80) _discount = 80; }
  });
}

// the client code
var sneakers = new Product("Sneakers",20);
sneakers.discount = 50; // 50, setter is called
sneakers.discount+= 20; // 70, setter is called
sneakers.discount+= 20; // 80, not 90!
alert(sneakers.discount); // getter is called

Note the last but one line: the responsibility for correct discount value was moved from the client code (e-shop definition) to the product definition. The product is responsible for keeping its data members consistent. Good design is (roughly said) if the code works the same way as our thoughts.

So much about properties. But javascript is different from pure Object-oriented languages like C# and codes the features differently:

In C#, transforming fields into properties is a breaking change, so public fields should be coded as Auto-Implemented Properties if your code might be used in separatedly compiled client.

In Javascript, the standard properties (data member with getter and setter described above) are defined by accessor descriptor (in the link you have in your question). Exclusively, you can use data descriptor (so you can't use i.e. value and set on the same property):

  • accessor descriptor = get + set (see the example above)
    • get must be a function; its return value is used in reading the property; if not specified, the default is undefined, which behaves like a function that returns undefined
    • set must be a function; its parameter is filled with RHS in assigning a value to property; if not specified, the default is undefined, which behaves like an empty function
  • data descriptor = value + writable (see the example below)
    • value default undefined; if writable, configurable and enumerable (see below) are true, the property behaves like an ordinary data field
    • writable - default false; if not true, the property is read only; attempt to write is ignored without error!

Both descriptors can have these members:

  • configurable - default false; if not true, the property can't be deleted; attempt to delete is ignored without error!
  • enumerable - default false; if true, it will be iterated in for(var i in theObject); if false, it will not be iterated, but it is still accessible as public

Learn by example:

var o = {};
Object.defineProperty(o,"test",{
  value: "a",
  configurable: true
});

for(var i in o) console.log(o[i]); // nothing, o.test is not enumerable
console.log(o.test); // "a"
o.test = "b"; // o.test is still "a", (is not writable, no error)
delete(o.test); // bye bye, o.test (was configurable)
o.test = "b"; // o.test is "b"
for(var i in o) console.log(o[i]); // "b", default fields are enumerable

You don't need to use this if you write just a few lines fun. But if you want to code a game (as you mentioned in the linked question), you should really care about good design. Try to google something about antipatterns and code smell. It will help you to avoid the situations like "Oh, I need to completely rewrite my code again!", it can save you months of despair if you want to code a lot. Good luck

转自:http://stackoverflow.com/questions/18524652/how-to-use-javascript-object-defineproperty


一款付费下载资源、付费下载源码、收费附件下载、付费阅读查看隐藏内容的WordPress主题,一款针对收费付费下载资源/付费查看内容/付费阅读/VIP会员免费下载查看/虚拟资源售卖的WordPress主题,一款为erphpdown而生的wp主题。集付费下载资源、付费查看内容于一体,包含体验VIP、包月VIP、包年VIP、终身VIP权限,用户推广拿提成提现,VIP免费下载资源、VIP每日下载次数限制,集成官方支付宝/微信支付接口、多个个人免签支付接口。 自适应响应式设计,兼容主流浏览器 网格样式与瀑布流样式任意切换 内置SEO优化 自带与主题UI完美兼容搭配的erphpdown前端用户中心页面(此功能若单独找我们定制也需要几百) 收费付费下载资源、付费查看内容、付费观看视频、付费下载音乐,上传文件支持第三方云存储(OSS、七牛云等插件) VIP权限下载与查看,VIP名称支持自定义 用户投稿上传资源售卖,赚取积分,可提现 用户推广系统(A推广B注册,B消费后A拿提成,可提现),可实现三级分销 每日签到送积分 兼容某免费问答插件,实现网站问答功能 在线工单 管理员群发站内信(站内通知) 无限自助广告位购买(用户可用积分购买广告位,自助投放广告),需使用ErphpAD插件(限时活动免费赠送) 白天/夜间模式切换(后台可设置自动切换、默认夜间模式、禁止切换)、繁简体切换 如果你需要做一个收费观看视频电影的网站,也可使用本主题,可设置成用户单独购买或升级VIP后观看! 如果你需要做一个付费下载音乐并且带试听的音频网站,也可以使用本主题。 如果你需要做一个可在线浏览ppt、pdf、word等office文档的网站,也可使用本主题!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值