JavaScript Object-oriented learning--4

Source: Internet
Author: User
Tags object serialization tojson hasownproperty

Creation of objects

1) literal, 2) new, 3) object.create ()

Access to Properties

1, can pass the point (.) or the square brackets ([") operator to get the value of the property. The left-hand side of the operator should be an expression that returns an object. For Point (.) , the right side must be a simple identifier named after the property name. For square brackets (")), the square brackets must be an expression that evaluates to a string, which is the name of the property. In ECMAScript 3, the identifier after the dot operator cannot be a reserved word, for example, o.for or o.class is illegal, because for is a JavaScript keyword, class is a reserved word. If the property name of an object is a reserved word, you must access them in the form of square brackets, for example. ["for"] and. ["Class"] o ECMAScript 5 relaxed the limit (including some implementations of ECMAScript 3), you can use the reserved word directly after the dot operator. When square brackets are used, we say that the expression in square brackets must return a string. In fact, more strictly speaking, an expression must return a string or return a value that can be converted to a string. In the 7th chapter there are some examples of the use of numbers in square brackets, which is like a very common case.

2. Suppose you want to query the object. The attribute x, if. does not exist in X, then it will continue in the. The prototype object in the query property x. If there is no X in the prototype object, but the prototype object also has a prototype, continue to execute the query on the prototype object's prototype until the X is found or if a prototype is a F1u11 object. As you can see, the prototype property of an object forms a "chain" that enables inheritance of attributes through this "chain".

Delete Property

The delete operator can 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).

Detection properties

1. JavaScript objects can be considered as collections of attributes, and we often detect the ownership of members in a collection-to determine whether a property exists in an object. This can be done through the in operator, the Hasownpreperty (), and the propertyIsEnumerable () method, even through property queries.

The left side of the 2, in operator is the property name (string), and the right side is the object. Returns true if the object's own property or inheritance property contains this property:

3. The hasOwnProperty () method of the object is used to detect whether a given name is an object's own property. For inherited properties It will return false:

4. propertyIsEnumerable () is an enhanced version of hasOwnProperty (), which returns true only if it detects its own property and the enumerable of this property (enumerable attribute) is true. Some of the built-in properties are not enumerable. Properties that are typically created by JavaScript code are enumerable

5. However, there is a scenario where you can use the in operator only and cannot use the above property access method. In can differentiate between non-existent properties and properties that exist but have a value of undefined

Three properties of an object

1. To detect whether an object is a prototype of another object (or in a prototype chain), use the isprototype0f () method. For example, you can use P.ISPROTOTYPE0F (o) to detect if P is a prototype of O.

2. The object's class attribute (class attribute) is a string that represents the type information of the object. Neither ECMASCRIPT3 nor ECMAScript 5 provides a way to set this property, and there is only one indirect way to query it. The default ToString () Method (inherited from Object.prototype) returns the following string in this format.

Object.prototype.toString.call (o). Slice (8,-1);

The extensibility of an object is used to indicate whether a new property can be added to an object. All built-in objects and custom objects are explicitly extensible, and the extensibility of the host object is defined by the JavaScript engine. In ECMAScript 5, all built-in objects and custom objects are extensible, unless they are converted to non-extensible, and the scalability of the host object is also defined by the JavaScript engine that implements ECMAScript 5.

Serialization of objects

1. Object serialization (serialization) refers to converting the state of an object to a string, or to restoring a string to an object. ECMAScript 5 provides t built-in functions json.stringify () and Json.parse () to serialize and restore JavaScript objects. These methods use JSON as the data Interchange format, and JSON is the full name of the "Javascriptobject Notation"-javascript object notation, and its syntax and JavaScript objects are very similar to the syntax of the array's direct volume:

2, JSON syntax is a subset of JavaScript syntax, it does not represent all the values in JavaScript. Supports objects, arrays, strings, infinity numbers, true, false, and null, and they can be serialized and restored. The result of nan,infinity and a infinity serialization is null, and the result of the Date object serialization is a JSON-formatted date string (referring to the Date.tojson () function), but Json.parse () retains their string morphology. They are not reverted to the original date object. Functions, REGEXP, Error objects, and undefined values cannot be serialized and restored. Jsdn.stringify () can only serialize an object's own property that can be enumerated. For a property that cannot be serialized, this property is omitted from the serialized output string. Both Js0n.stringify () and Json.parse () can receive a second optional parameter to customize a custom serialization or restore operation by passing in a list of properties that need to be serialized or restored.

Methods of the Object

1, hasOwnProperty (). propertyIsEnumerable () and isprototype0f () are the three methods, as well as the static function object defined in the object constructor.

Methods such as Create () and object.getprototype0f () need to be understood.

2. The toString () method has no arguments, and it returns a string representing the value of the object that called the method. JavaScript calls this method when the object needs to be converted to a string. For example, when you use the "+" operator to concatenate a string and an object, or if an object is used in a method that expects to use a string, ToString () is called.

3. Except for the basic ToString () method, the object contains the toLocaleString () method, which returns a localized string representing the object. The default toLocaleString () method in object does not do anything to localize itself, it only calls the ToString () method and returns the corresponding value. The date and number classes have been customized for the toLocaleString () method, and can be used to localize conversions of numbers, dates, and times. The toLocaleString () method of the array class is much like the to5tring () method, and the only difference is that each array element calls the toLocaleString () method to convert to a string instead of calling the respective ToString () method.

4. Object.prototype does not actually define the Tojson () method, but for objects that need to perform serialization, The Json.stringify () method invokes the Tojson () method. If the method exists in the object to be serialized, it is called, and the return value is the result of the serialization, not the original object. See Date.tojson () for a specific example.

5, the value0f () method is very similar to the ToString () method, but it is often called when JavaScript needs to convert an object to a primitive value rather than a string, especially when it is converted to a number. If you use an object in the context where you want to use the original value, JavaScript automatically calls this method. The default value0f () method is not surprising, but some built-in classes customize the}value0f () method (such as date.value0f ()), and 9.6.3 discusses how to define a value0 generation for a custom object type) method.


JavaScript Object-oriented learning--4

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.