Making Properties nonenumerable

Source: Internet
Author: User

We implemented a set class by using "Object ID" properties to map objects. every object has a unique object ID. in order to ensure the same object is added only once, we make the ID a property of the object itself. so we bypass the addition if there exists the same ID property in the set as the property of the object being added. otherwise, we generate its ID property and add the object to the set. when in a for/in loop, the property will be enumerated inevitably. but here comes the problem: the ID property has no more significance than sign of whether the Object ID has been queried. as a result, we more often want it to be nonenumerable. here is the situation where object. defineproperty () comes into play.



We can simply use object. defineproperty () to define the new property like this:


In this way, the | ** objectid ** | property will not be revealed by the for/in loop.
However, it is somewhat inconvenient when we recommend CT an object to have such a property in other cases because we have to call set. _ V2s which might be irrelevant.
So, we simply make the ID property a property of the prototype of object so that it will be inherited by all objects. how to make sure that every object has an ID that is unique since the ID property is inherited by all objects? Good catch! We achieve this in the above codes by defining the property only when the property is queried so that we can base on the value increment to make the property unique. this enlightens us that the ID property is not necessarily a value property. it can be a getter to reveal the actual ID property. if the ID property is there, just return it, and if not, create one.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.