JS in six types of data (vi)--object

Source: Internet
Author: User

The object in ECMAScript is a mutable keyed collection (that is, a collection of data and functions). It aggregates a number of values together, which can be accessed by name. Objects can also be considered as containers for properties, each of which is a name/value pair. The name of the property can be any string, including an empty string. The property value can be any value other than the undefined value. The most common uses of objects are creating (create), setting (set), finding (query), deleting (delete), detecting (test), and enumerating (enumerate) his properties.
First, the attribute typeECMA-262 version 5th describes the various characteristics of attributes when defining features that are only used internally. In order to represent the intrinsic values of an attribute, the specification places them in two opposing brackets, for example: [[Enumerable]]. There are two types of properties in the ECMAScript object: Data properties and accessor properties. 1. Data PropertiesThe Data property contains the location of a data value. Values can be read and written at this location.     The data attribute has 4 attributes that describe its behavior. A.[[configurable]]: Indicates whether the property can be redefined by deleting the property from the delete, whether the attribute is modified, or whether the property can be modified to an accessor property.     The default value of this property is true. B.[[enumerable]]: Indicates whether the property can be returned through the for-in loop.     The default value of this property is true. C.[[writable]]: Indicates whether the value of the property can be modified.     The default value of this property is true. D.[[value]]: Contains data values for this property. When reading a property value, read from this position, and when writing the value of the property, save the new value in this position. The default value for this attribute is undefined. 2. Accessor properties:A.[[configurable]]: Indicates whether the property can be redefined by deleting the property from the delete, whether the attribute is modified, or whether the property can be modified to a data property.     The default value for this attribute is true for properties that are defined directly on the object. B.[[enumerable]]: Indicates whether the property can be returned through the for-in loop.     The default value for this attribute is true for properties that are defined directly on the object. C.[[get]]: The function that is called when the property is read.     The default value is undefined. D.[[set]]: The function that is called when the property is written. The default value is undefined.   second, create the object1. Create and initialize a new object by using new Create object new operator. Keyword new follows a function call. The function here is called a constructor, and the constructor is used to initialize a newly created object. Primitive types in the JavaScript language core contain built-in constructors. For example:
In addition to these built-in constructors, it is very common to initialize new objects with custom constructors.   2. Object Literals The simplest way to create an object is to use the object literal in JavaScript code. The object literal is a mapping table consisting of several name/value pairs, the middle of the name/value pair separated by a colon, the name/value pair separated by commas, and the entire mapping table enclosed in curly braces, with the following structure: {property Name 1: Property value 1, property Name 2: Property value 2, ...}. The property name can be either a JavaScript identifier or a string literal (including an empty string). The value of the property can be any type of JavaScript expression, and the value of the expression (which can be either the original value or the object value) is the value of this property. Take a look at the example:

3. Prototypes each object we create has a prototype (prototype) attribute, which is a pointer to an object that is used to contain properties and methods that can be shared by all instances of a particular type. If it is understood literally, then prototype is the prototype object of the object instance created by invoking the constructor. The advantage of using a prototype object is that you can have all object instances share the properties and methods that it contains. In other words, instead of defining the information for an object instance in the constructor, you can add that information directly to the prototype object, see the example:

Here, we add the Sayname () method and all properties directly to the prototype property of the person, and the constructor becomes an empty function. Even so, you can still create new objects by calling the constructor, and the new objects will have the same properties and methods. But unlike constructors, these properties and methods of the new object are shared by all instances of the。 In other words, Person1 and Person2 access both the same set of properties and the same sayname () function. Since the object's prototype involves a lot of knowledge and complexity, it is not explained here, I will be in a future article for you to detail the concept of prototype (prototype). In addition, JavaScript has a object.create () method to create objects. III. Various operations based on object attributes1. Object properties get access to object properties you can use dot notation and square bracket notation. For a point (.), the right side must be a simple identifier named after the property name. In the case of square brackets ([]), the square brackets must be an expression that evaluates to a string, which is the name of the property. Take a look at the example:

When square brackets are used, we say that the expression in square brackets must return a string. In fact, more strictly speaking, the expression must return a string or return a value that can be converted to a string (this feature can be used to handle many situations).     From this we can think of arrays, which are very similar to the method of getting elements, in fact, the array is also an object, but its property name is an array subscript. 2. Property Access Error Property Access does not always return or set a value. Querying a non-existent property does not make an error, and if attribute X is not found in either the object's own property or the inherited property, the property access expression o.x returns undefined. Recall that our person object has the property "name" and no Property "SubName": But if the object does not exist, then attempting to query the property of the non-existent object will cause an error.        Null and undefined values have no properties, so querying the properties of these values will result in an error, see example: Unless you are sure that both person and person.subname are (or are in the act of) an object, you cannot write such an expression person.subname.length, as this will cause an error, and here are two ways to avoid errors:



The second example uses the use of a composite language idiom of "&&", so that only the value after the person is true (cannot be null or undefined) is computed as a "short-circuit",&& behavior. Of course, setting properties to null and undefined will also report type errors. Setting properties for other values is also not always successful, there are some properties that are read-only, cannot be re-copied, and some objects do not allow new properties, but surprisingly, the failed actions of these settings properties do not give an error:


This is a legacy issue, and this bug has been fixed in the strict mode of ECMASCRIPT5.      In strict mode, any failed property set operation throws a type error exception. 3. Delete an attribute delete operator to delete an object's properties. Its operand should be a property-access expression. Surprisingly, delete simply disconnects the property from the host object and does not manipulate the properties in the property.

The     delete operator can only delete its own property and cannot delete an inherited property (to delete an inherited property must remove it from the prototype object that defines the property, and this affects all objects that inherit from the prototype)     It returns True when the delete expression is removed successfully or without any side effects, such as deleting a property that does not exist. If the delete is not a property-access expression, delete also returns true:            Delete cannot delete properties that are configurable as false (although you can delete non-extensible configurable properties). The properties of some built-in objects are not configurable, such as the properties of global objects created through variable declarations and function declarations. In strict mode, deleting a non-configurable property will report a type error. In non-strict mode, the delete operation in these cases returns false:           When you delete a configurable property of a global object in non-strict mode, you can omit a reference to the global object, and follow the delete operation specifier the name of the property you want to delete:           Then in strict mode, after the delete follows an illegal operand (such as x), a syntax error is reported, so the object and its properties must be explicitly specified:              4. Enumeration Properties     for-in statements can be used to traverse all property names in an object. The enumeration process will list all of the attributes-including the ones in the function and the prototypes you might not care about-so it is necessary to filter out the values you don't want. The most commonly used filters are the hasOwnProperty method, and the use of typeof to exclude functions:            The order in which property names appear is indeterminate, so be prepared for any order that might occur. If you want to make sure that attributes appear in a particular order, the best way to avoid using the for-in statement is to create an array in which to include the property name in the correct order:             can get the properties we want by using for instead of for-in, without worrying about discovering the properties in the prototype chain, And we get their values in the right order.






View more articles please follow the public number: Wcs290130--javascript those things. JavaScript Technology Exchange Group: 344292264.

JS in six types of data (vi)--object (EXT)

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.