原创转载请注明出处:http://agilestyle.iteye.com/blog/2341900
Property attributes
All properties have several associated attributes. These attributes define how the properties work.
Common Attributes
Both data and accessor properties have [[Enumerable]] and [[Configurable]] attributes.
var person1 = { name: "Nicholas" }; Object.defineProperty(person1, "name", { enumerable: false }); console.log("name" in person1); // true console.log(person1.propertyIsEnumerable("name")); // false var properties = Object.keys(person1); console.log(properties.length); // 0 Object.defineProperty(person1, "name", { configurable: false }); // try to delete the Property delete person1.name; console.log("name" in person1); // true console.log(person1.name); // Object.defineProperty(person1, "name", { // error!!! // configurable: true // });
Data Property Attributes
Data properties also have [[Writable]] and [[Value]] attributes,
var person1 = {}; Object.defineProperty(person1, "name", { value: "Nicholas", enumerable: true, configurable: true, writable: true });
while accessor properties have [[Get]] and [[Set]] attributes.
var person1 = { _name: "Nicholas" }; Object.defineProperty(person1, "name", { get: function () { console.log("Reading name"); return this._name; }, set: function (value) { console.log("Setting name to %s", value); this._name = value; }, enumerable: true, configurable: true }); console.log("name" in person1); // true console.log(person1.propertyIsEnumerable("name")); // true delete person1.name; console.log("name" in person1); // false person1.name = "Greg"; console.log(person1.name); // "Greg"
Note:
By default, [[Enumerable]] and [[Configurable]] are set to true for all properties, and [[Writable]] is set to true for data properties.
Defining Multiple Properties
You can change these attributes by using Object. defineProperty() or Object.defineProperties().
It's also possible to define multiple properties on an object simultaneously if you use Object.defineProperties() instead of Object.defineProperty(). This method accepts two arguments: the object to work on and an object containing all of the property information. The keys of that second argument are property names, and the values are descriptor objects defining the attributes for those properties.
For example, the following code defines two properties:
var person1 = {}; Object.defineProperties(person1, { // data property to store data _name: { value: "Nicholas", enumerable: true, configurable: true, writable: true }, // accessor property name: { get: function () { console.log("Reading name"); return this._name; }, set: function (value) { console.log("Setting name to %s", value); this._name = value; }, enumerable: true, configurable: true } });
Retrieving Property Attributes
It’s also possible to retrieve these attributes by using Object.getOwnPropertyDescriptor().
var person1 = { name: "Nicholas" }; var descriptor = Object.getOwnPropertyDescriptor(person1, "name"); console.log(descriptor.enumerable); // true console.log(descriptor.configurable); // true console.log(descriptor.writable); // true console.log(descriptor.value); // "Nicholas" var person2 = {}; Object.defineProperty(person2, "name", { value: "Nicholas" }); var desc = Object.getOwnPropertyDescriptor(person2, "name"); console.log(desc.enumerable); // false console.log(desc.configurable); // false console.log(desc.writable); // false console.log(desc.value); // "Nicholas"
Reference
Leanpub.Principles.of.Object-Oriented.Programming.in.JavaScript.Jun.2014
相关推荐
Work with a combination of access modifiers, prefixes, properties, fields, attributes, and local variables to encapsulate and hide data Master DOM manipulation, cross-browser strategies, and ES6 ...
propertyGrid1.SelectedObject = myObject; } } public class MyClass { } public class DynamicPropertyDescriptor : System.ComponentModel.PropertyDescriptor { public DynamicPropertyDescriptor(string ...
总结来说,手动动态添加`PropertyGrid`的数据行并显示在C#中涉及的关键技术有:`ICustomTypeDescriptor`接口的实现、自定义`PropertyDescriptor`类以及正确设置`PropertyGrid`的`SelectedObject`属性。这种非属性...
例如,你可以创建一个自定义`PropertyDescriptor`子类,覆盖其`CanResetValue`、`GetValue`、`SetValue`等方法,然后在运行时将它添加到`PropertyGrid`的`Attributes`集合中。 2. **Globalization**: 全球化...
在JavaScript中,`attribute` 和 `property` 是两个与DOM元素相关的概念,它们虽然有所关联,但具有明显的差异。理解这两个概念的区别对于JavaScript开发者来说至关重要,尤其是对于新手来说,混淆这两个概念是常见...
压缩包内的文件“Object_Attributes_e.pdf”和“Object_Attributes_d.pdf”可能分别代表英文版和描述文档或代码示例。通过阅读这些文件,开发者可以深入理解WPF中的对象属性如何影响用户界面的呈现和交互性。 以下...
Attributes结构在Java编程中是一个非常重要的概念,尤其对于理解类文件的元数据有着关键的作用。在Java类文件中,每个字段、方法或者类定义都可以有零个或多个属性(Attributes)。这些属性提供了额外的信息,例如...
属性信息存储在`attributes`属性中,这是一个Object类型的键值对集合,用来存储图形的各种元数据。然而,在实际应用中,特别是需要将这些属性展示在DataGrid控件中时,会遇到一些挑战,因为`attributes`对象不能直接...
protected override PropertyDescriptor CreateProperty(MemberInfo member, object instance) { var pd = base.CreateProperty(member, instance); if (pd.PropertyType.IsEnum) return new ...
在JavaScript中,`property`指的是DOM节点对象上的属性,它们是JavaScript对象的一部分,可以用来获取或设置元素的特性。比如,对于HTML元素,`id`、`title`、`lang`、`dir`和`className`都是标准的`properties`。...
6.7 Property Attributes 131 6.8 Object Attributes 135 6.9 Serializing Objects 138 6.10 Object Methods 138 7. Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...
JavaScript中的Object对象是语言的核心部分,它提供了许多用于操作和管理对象的方法和属性。这篇教程主要探讨了如何使用和理解Object对象。 首先,Object构造函数是创建新对象的关键。当你使用`new Object()`或者...
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { // 实现下拉列表逻辑 } } ``` 然后,在需要的属性上添加`EditorAttribute`: ```csharp ...
Chapter 10: Properties and Events Chapter 11: Interfaces Chapter 12: Manipulating Classes Chapter 13: Objects and Memory Part III Chapter 14: Generics Chapter 15: Anonymous Methods Chapter 16: ...
XmlBuilder是一个针对XmlDocument对象的类库函数,主要目的是为了方便开发者更高效、更便捷地访问和操作XML文档的核心元素——DocumentElement,以及其包含的属性Attributes。在处理XML数据时,XmlDocument是.NET...
John Resig is an acknowledged JavaScript authority and the creator of ...Cutting through attributes, properties, and CSS PART 4 MASTER TRAINING Surviving events Manipulating the DOM CSS selector engines
每个XML文档都包含三部分:声明(Document Type Declaration, DTD)、元素(Elements)和属性(Attributes)。声明部分可选,用于指定文档的版本和是否允许不严格的解析。元素是XML的基石,它们通过开始标签和结束...