Episode 4 of JavaScript Learning

Source: Internet
Author: User
Tags hasownproperty

 

In this chapter, we will discuss objects and arrays in JavaScript.

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

7. Add the memory usage of the array:
A [10] = "test"; // Add a new element
Memory usage:
For example:
A [0] = "1 ";
A [10] = "10 ";
Then, the JS value allocates memory to the elements with subscripts 0 and 10, and the nine elements in the middle are not allocated;

Note: Arrays can also be added to objects;
For example;
VaR A = new circle (1, 2, 3 );
A [0] = "test ";
This example defines a new object property named "0.
Adding an array element to an object does not make it an array.

8. delete an array:
VaR A = [1, 2];
Delete A [0];
Alert (A [0]) // output undefined
Alert (A [1]) // output 2
The example shows that the delete operation does not actually Delete the object, but sets the element to undefined;

If you want to delete it, you can use array. Shift () and other methods.
For example:
VaR A = [1, 2];
Delete A [0];
Alert (A [0]) // output undefined
Alert (A [1]) // output 2
A. Shift (); // Delete the first element of the array
Alert (A [0]) // output 2
Alert ("Length:" + A. Length );
Alert (A [1]) // output undefined; 1 has been deleted. In fact, the length of the array is only 1;

9, length of the array:
A [49] = "";
// The length of the array is 50;

The Length attribute is often used to traverse array elements;
For example:
VaR A = ["A", "B", "C"];
For (VAR I = 0; I <A. length; I ++ ){
Alert (A [I]);
}

It is assumed that the element is continuous, if not:
You must check whether each element is defined, for example:
For (VAR I = 0; I <A. length; I ++ ){
If (A [I]) {
Alert (A [I]);
}
}

Multi-dimensional array: A [I] [J];

10. array methods:
1): Join () method:
Converts all elements of an array to a string.
For example, var a = [1, 2, 3];
VaR S = A. Join (); // output S = 1, 2, 3
You can also specify a separator;
For example;
S = A. Join (",");
This method is opposite to string. Split (). Split () Splits a string into several fragments to create an array;

2): reverse () method:
Reversing an array.
VaR A = new array (1, 2, 3 );
A. Reverse ();
VaR S = A. Join (); // s = "3, 2, 1"

3): Sort () method:
Sort
♂: If no parameter is specified, the array elements are sorted alphabetically.
VaR A = new array ("ee", "DF", "B ");
A. Sort ()
VaR S = A. Join (","); // s = "B, DF, ee"
♂: If the parameter is set:
For example:
VaR A = [33,4, 1111,222]
A. Sort (); // sorting: 1111,222, 33,4
A. Sort (function (x, y ){
Return x-y;
});
VaR S = A. Join (); // output 4, 33,222,111 1

// As shown in the example, if X> Y is sorted, the first parameter is placed after the first parameter,
For example, if 1111,222-1111-222> 0-222,111, then 1
Note the alphabetic order: Because Javascript is case-sensitive, the characters are sorted in uppercase or lowercase.

4): Concat () method:
VaR A = [1, 2, 3];
A = A. Concat (4, [5, 6], 7 );
A = A. Join (); // output 1, 2, 3, 4, 5, 6, 7
Alert ()
Note:
If there is an array in the array, it cannot be expanded.
For example:
VaR A = [1, 2, 3];
A = A. Concat (4, [5, [6, 6], 7 );
Alert (a); // This cannot be seen
A = A. Join ("| ");
Alert (a); // after segmentation, note that there is a comma

-------------------------------------

VaR c = [1, 2, 3];
VaR d = new array (1, 2, 3 );
Alert (c); // 1, 2, 3
Alert (d); // 1, 2, 3
// The reason why the object is not output is that
// Array is an object with an extra function layer.
// We remember its particularity.

5): slice () method:
Returns a part of the array. Similar to the substring method of a string.

6): splice () method:
First, he and slice have only one letter difference, but the use is completely different.
It can be used to delete objects.
VaR A = [1, 2, 3];
A = A. splice (0, 2 );
Alert (a); // output 1, 2
A = A. splice (1, 2 );
Alert (a); // Output 2. If it is a = A. splice (0, 1); Output 1
A = A. splice (1, 2 );
Alert (a); // No array is deleted and an empty array is output.

You can also insert an array. Specific Method:
VaR array1 = new array ("1", "2", "3", "4 ");
Array1.splice (2nd, "5"); // after the first element, insert 5. If the fifth parameter is 0, do not delete it.
Document. Write (array1 + "<br>"); // output 1, 5, 2, 3, 4
Array1.splice (3rd, "7", "8") // Delete the three elements after the first element. That is, 3rd, 4th, and 5th elements. Insert
Document. Write (array1); // output 1, 5, 7, 8
Note: Unlike Concat (), splice does not expand the inserted parameters. That is, if an array is inserted, It is inserted into the array itself, and it is not an array element.
If Concat () is inserted into an array, it will expand the array and insert the elements in the array. However, when the inserted array is
When there is an array, it will not be expanded.


7): Push () and POP () methods:
Push (): attaches one or more arrays to the end of the array.
Pop (): Delete the last element of the array.
VaR array1 = new array ("1", "2", "3", "4 ");
Array1.push ("5 ");
Document. Write (array1 + "<br>"); // output 1, 2, 3, 4, 5
Array1.pop ()
Document. Write (array1); // output 1, 2, 3, 4

8): unshift () and shift () methods:
Different from push and pop...
Unshift (): attaches one or more arrays to the header of the array.
Shift (): deletes the first element of the array.

There are many methods for arrays, which seem annoying. So everyone should be patient.

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