Making Properties Nonenumerable

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 a 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 expect 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.

Making Properties Nonenumerable

时间: 2024-11-13 06:58:14

Making Properties Nonenumerable的相关文章

springboot的application.properties与.yml的区别

现在我们的application.properties文件内容是: [plain] view plain copy server.port=8090 server.session-timeout=30 server.context-path= server.tomcat.max-threads=0 server.tomcat.uri-encoding=UTF-8 spring.datasource.url = jdbc:mysql://localhost:3306/newbirds spring

Properties

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Map; import java.util.Map.Entry; import java.uti

Spring用代码来读取properties文件

我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org.springframework.beans.factory.config.PropertyPlaceholderConfigurer <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

给你的JAVA程序配置参数(Properties的使用)

我们在写JAVA程序时,很多时候运行程序的参数是需要动态改变的 测试时一系列参数,运行时一系列参数 又或者数据库地址也需要配一套参数,以方便今后的动态部署 这些变量的初始化,我们在写小DEMO时完全可以写死在JAVA文件中 但程序需要发布或者局部部署时,这些参数就需要脱离程序代码了 我们有多种存放参数的方式,比如数据库.XML文件又或者直接是txt文件 现在介绍一种使用JAVA,简单方便的参数读取方式 .properties文件,我们并不陌生,很多优秀的框架中就能看到它的存在,比如Hiberna

java 通过 Properties 读取数据库配置 .properties 文件的使用。

system.properties user=root password=root jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8; driverClass=com.mysql.jdbc.Driver 调用方法: public void getProperties(){ Properties prop = new Properties(); try { prop.load(new B

properties文件路径的读取

System.out.println(System.getProperty("user.dir"));  //这个是去工程的绝对路径的  System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));//这个是去当前classpath的uri的!  new Properties().load(new FileInputStream("test.prope

spring 读取properties的两种方法

一:直接使用context命名空间 如: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:websocket="http

Eclipse的properties插件

分享一个不错的编写properties文件的Eclipse插件(plugin),有了它咱们在修改一些简体中文.繁体中文等 Unicode文本时,就不用再运用native2ascii编码了.您能够经过Eclipse中的软件晋级(Software Update)装置此插件,过程如下: 1.打开Eclipse的Help菜单,将鼠标移到Software Update子项,在呈现的子菜单中点击Find and Install:2.在Install/Update对话框中挑选Search for new fe

JAVA使用和操作properties文件

java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便.Properties 类存在于包 Java.util 中,该类继承自 Hashtable. 1. getProperty ( String  key) ,   用指定的键在此属性列表中搜索