Javascript learning 4-objects and Arrays

Source: Internet
Author: User

In JavaScript, objects and arrays are two basic data types, and they are also the two most important data types.
An object is a set of named values, and an array is a special object, which is like a set of ordered values.

4.1 objects as associative arrays
For an object, its attributes are equivalent to a set of named string values. You can use the array access operator [] to extract attributes.
An object can use "." to access an object attribute. The more common access attribute operator of an array is []. The following two expressions are equivalent:

1 Object. Property
2 Object [ " Property " ]

An important difference between the two methods is that the property name of the former is an identifier, and the property name of the latter is a string.★
The following Original article illustrates its importance:
In C, C ++, Java, and similar stronugly typed versions, an object can have only a fixed number of properties, and the names of these properties must be defined in advance. since Javascript is a loosely typed language, this rule does not apply: a program can create any number of properties in any object. when you use. operator to access a property of an object, however, the name of the property is expressed as an identifier. identifiers must be typed literally into your JavaScript program; they are not a datatype, so they cannot be manipulated by the program
On the other hand, when you access a property of an object with the [] array notation, the name of the property is expressed as a string. strings are Javascript datatypes, so they can be manipulated and created while a program is running. so, for example, you can write the following code in javascript:

1 VaR ADDR =   "" ;
2 For (I =   0 ; I <   4 ; I ++ ) {
3ADDR+ =Customer ["Address" +I]+ '\ N';
4}

4.2 Common Object Attributes and Methods
① Constructor attributes
References the construction of the property initialization object.
② Tostring () method
This method is called when an object is converted to a string.
③ Tolocalstring () method
Returns a localized string representation.
④ Valueof () method
Similar to the tostring () method, it is called when JavaScript converts an object to a certain basic data type, that is, a number rather than a string.
The default valueof does not make any sense.
⑤ Hasownproperty () method
The hasownproperty () method returns true if the object locally defines a noninherited property with the name specified by the single string argument. Otherwise, it returns false. For example:

1 VaR O =   {} ;
2 O. hasownproperty ( " UNDEF " ); // False: The property is not defined
3 O. hasownproperty ( " Tostring " ); // False: tostring is an inherited property
4 Math. hasownproperty ( " Cos " ); // True: The math object has a cos Property

That is to say, if the string specified in the parameter is equivalent to an attribute of the object, this attribute is implemented in this class, true is returned, and false is returned in the inheritance class.
⑥ Propertyisenumerable () method
If the name specified by the string parameter is equivalent to a non-inherited attribute of the object, and the attribute can be enumerated in a for/in loop. Returns true.

1 VaR O =   {X:1} ;
2 O. propertyisenumerable ( " X " ); // True: property exists and is enumerable
3 O. propertyisenumerable ( " Y " ); // False: Property doesn' t exist

Note that all user-defined properties of an object are enumerable. (all user-defined properties of an object can be enumerated .) Nonenumerable properties are typically inherited properties (see Chapter 9 for a discussion of property inheritance), so this method almost always returns the same result as hasownproperty ().
7. isprototypeof () method
The isprototypeof () method returns true if the object to which the method is attached is the prototype object of the argument. Otherwise, it returns false. For example:

1 VaR O =   {}
2 Object. Prototype. isprototypeof (O ); // True: O. constructor = Object
3 Object. isprototypeof (O ); // False
4 O. isprototypeof (object. Prototype ); // False
5 Function. Prototype. isprototypeof (object ); // True: object. constructor = Function

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.