如何使用 javascript Object.defineProperty [英] how to use javascript Object.defineProperty

查看:35
本文介绍了如何使用 javascript Object.defineProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我四处寻找如何使用 Object.defineProperty 方法,但找不到合适的方法.

I looked around for how to use the Object.defineProperty method, but couldn't find anything decent.

有人给了我这段代码:

Object.defineProperty(player, "health", {
    get: function () {
        return 10 + ( player.level * 15 );
    }
})

但我不明白.主要是, get 是我无法获得的(双关语).它是如何工作的?

But I don't understand it. Mainly, the get is what I can't get (pun intended). How does it work?

推荐答案

既然你问了一个类似的问题,我们来看看就一步一步来吧.它有点长,但它可能比我花在写这个上的时间节省更多的时间:

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

属性 是一种 OOP 功能,旨在将客户端代码完全分离.例如,在某些电子商店中,您可能有这样的对象:

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; ... }

后来,网店老板可能会意识到折扣不能超过 80%.现在您需要在客户端代码中找到每次出现的折扣修改并添加一行

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

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

然后网店老板可能会进一步改变他的策略,比如如果客户是经销商,最大折扣可以是 90%".而且您需要再次在多个地方进行更改,而且您需要记住在更改策略时随时更改这些行.这是一个糟糕的设计.这就是为什么封装是OOP的基本原则.如果构造函数是这样的:

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 anytime 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; } 
}

然后你可以改变 getDiscount (accessor) 和 setDiscount (mutator) 方法.问题是大多数成员的行为就像普通变量,只是折扣在这里需要特别注意.但是好的设计需要封装每个数据成员以保持代码的可扩展性.所以你需要添加很多什么都不做的代码.这也是一个糟糕的设计,一个样板反模式.有时你不能只是在以后将字段重构为方法(eshop 代码可能会变大或者一些第三方代码可能依赖于旧版本),所以这里的样板是不那么邪恶的.但是,它仍然是邪恶的.这就是为什么将属性引入许多语言的原因.您可以保留原始代码,只需将折扣成员转换为带有 getset 块的属性:

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;
//this.discount = 0; // <- remove this line and refactor with the code below
  var _discount; // private member
  Object.defineProperty(this,"discount",{
    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.

关于属性的内容就这么多.但是 javascript 与 C# 等纯面向对象语言不同,并且对功能的编码也不同:

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

在 C# 中,将字段转换为属性是一个 重大更改,因此公共字段应编码为 Auto-Implemented Properties 如果您的代码可能用于单独编译的客户端.

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 the separately compiled client.

在 Javascript 中,标准属性(具有上述 getter 和 setter 的数据成员)由访问器描述符(在您问题中的链接中)定义.独占地,您可以使用数据描述符(因此您不能在同一属性上使用即valueset):

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):

  • 访问器描述符 = get + set(见上面的例子)
    • get 必须是一个函数;其返回值用于读取属性;如果没有指定,默认是 undefined,它的行为就像一个返回 undefined 的函数
    • set 必须是一个函数;在给属性赋值时,它的参数用 RHS 填充;如果未指定,则默认为 undefined,其行为类似于空函数
    • 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
      • value 默认未定义;如果 writableconfigurableenumerable(见下文)为真,则该属性的行为就像一个普通的数据字段
      • 可写 - 默认false;如果不是true,则该属性是只读的;尝试写入被忽略,没有错误*!
      • 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:

      • 可配置 - 默认false;如果不为真,则无法删除该属性;尝试删除被忽略,没有错误*!
      • enumerable - 默认false;如果为 true,它将在 for(var i in theObject) 中迭代;如果为 false,它将不会被迭代,但它仍然可以作为 public 访问
      • 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

      * 除非在严格模式- 在这种情况下,JS 停止执行 TypeError,除非它被捕获在 try-catch 块

      * unless in strict mode - in that case JS stops execution with TypeError unless it is caught in try-catch block

      要读取这些设置,请使用 Object.getOwnPropertyDescriptor().

      To read these settings, use Object.getOwnPropertyDescriptor().

      通过例子学习:

      var o = {};
      Object.defineProperty(o,"test",{
        value: "a",
        configurable: true
      });
      console.log(Object.getOwnPropertyDescriptor(o,"test")); // check the settings    
      
      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
      

      如果您不想让客户端代码进行此类作弊,您可以通过三级限制来限制对象:

      If you don't wish to allow the client code such cheats, you can restrict the object by three levels of confinement:

      • Object.preventExtensions(yourObject) 防止将新属性添加到 yourObject.使用 Object.isExtensible() 检查对象是否使用了该方法.预防是(阅读下文).
      • Object.seal(yourObject) 同上且属性不能被移除(有效地将 configurable: false 设置为所有属性).使用 Object.isSealed() 检测对象上的此特征.密封件(阅读下文).
      • Object.freeze(yourObject) 同上且属性不可更改(有效地将writable: false 设置为所有具有数据描述符的属性).Setter 的可写属性不受影响(因为它没有).冻结是:这意味着如果属性是对象,它的属性不会被冻结(如果你愿意,你应该执行类似深度冻结"的操作,类似于 深度复制 - 克隆).使用 Object.isFrozen() 检测它.
      • Object.preventExtensions(yourObject) prevents new properties to be added to yourObject. Use Object.isExtensible(<yourObject>) to check if the method was used on the object. The prevention is shallow (read below).
      • Object.seal(yourObject) same as above and properties can not be removed (effectively sets configurable: false to all properties). Use Object.isSealed(<yourObject>) to detect this feature on the object. The seal is shallow (read below).
      • Object.freeze(yourObject) same as above and properties can not be changed (effectively sets writable: false to all properties with data descriptor). Setter's writable property is not affected (since it doesn't have one). The freeze is shallow: it means that if the property is Object, its properties ARE NOT frozen (if you wish to, you should perform something like "deep freeze", similar to deep copy - cloning). Use Object.isFrozen(<yourObject>) to detect it.

      如果你只写几行有趣的,你就不需要为此烦恼.但是如果你想编写一个游戏(正如你在链接问题中提到的),你应该关心好的设计.尝试在谷歌上搜索有关反模式代码异味的信息.它将帮助您避免诸如哦,我需要再次完全重写我的代码!"这样的情况,如果您想编写大量代码,它可以为您节省数月的绝望.祝你好运.

      You don't need to bother with 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 care about good design. Try to google something about antipatterns and code smell. It will help you to avoid 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.

      这篇关于如何使用 javascript Object.defineProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆