Javascript beginners Article 4 js objects and arrays page 1/2

Source: Internet
Author: User
Tags hasownproperty

It's hard. Prepare yourself ...... Take a deep breath...

1, for/in:
A Method for Traversing (enumerative) object attributes can loop through attributes we do not know beforehand.
It can enumerate all user-defined attributes, but cannot enumerate some predefined attributes and methods.
Attributes that cannot be enumerated are generally inherited attributes.

Delete the attributes of an object:
Delete book. width;
If you delete an attribute from an object, for/in does not enumerate the attribute, and width in book does not detect the attribute.

Another important purpose of for/in is to use it with the associated array: (if you forget the definition of the associated array, you can refer to the previous chapter .)
For (stoct in port ){
Value + = get_value (stoct) * port [stoct];

2. Common Object Attributes and methods:
1): constructor attributes:
Each object has this attribute, which references the constructor that initializes this object.
For example:
Var d = new Date (); // use Date () to create an object d;
D. constructor = Date; // true // attribute d. constructor references Date;

This attribute helps determine the type of an object;
For example:
We want to determine whether the type of a value is Date:
If (typeof o = "object") & (o. constructor = Date )){
// First check whether it is an object and then check whether Date is referenced

The above code can also be written as follows:
If (typeof o = "object") & (o instanceof Date )){
// The instanceof operator is used to detect the value of the o. constructor attribute.

3. toStirng () and toLocaleString () methods:
1): toLocaleStirng () returns a localized string of the object.
ToString and toLocaleString are returned in the same way, but in the subclass, there is a difference:
For example:
Array, Date, and Number define the toLocaleString () method for returning localized values.

4. hasOwnProperty () and propertyIsEnumerable () methods:
1): hasOwnProperty
Var a = {x: 1, y: 2 };
Var k = a. hasOwnProperty ("x ");
Alert (k) // true
Alert (Math. hasOwnProperty ("z"); // false
Alert (Math. hasOwnProperty ("cos"); // true
Note: Math, cos (): calculates in radians and returns the cosine of the specified angle.
PropertyIsEnumerable () is the same as the returned result with hasOwnProperty;

4. isPrototypeOf () method:
If the object to which the method belongs is the prototype object of the parameter.
Var a = {x: 1, y: 2 };
Var k1 = Object. prototype. isPrototypeOf (a); // o. constructor = Object
Var k2 = Object. prototype. isPrototypeOf (Function); // Function. constructor = Object
Alert (k1) // true
Alert (k2) // true

5, array:
1) Create an array:
Direct array quantity:
Var es = [];
Complex point var es = [[1, {x: 1, y: 2}], [2, {x: 3, y: 4}];
Another method is to use Array () to construct a function:
V1: No parameter:
Var a = new Array ();
Empty array, equal to var a =;

V2: multiple parameters:
Var a = new Array (1, 2, 3, "tt"); // you can see that the direct definition is simpler.

V3: 1 numeric parameter:
Var a = new Array (3 );
An array with three elements. The value of each element is undefined;

6. subscript (INDEX) of the array ):
Size: 0 <= subscript <2 to the power of 32-1;
If it is out of the range, js will convert it into a string as the name of the object property;
Instead of as the subscript of the array;
For example:
A [-1.2] = "test"; // equivalent to a ["-1.2"] = "test ";
// Code explanation: Create an attribute named "-1.2" instead of defining a new array element.

Related Article

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: 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.