JavaScript Getting Started learning fourth JS object and array 1th/2 Page _ Basics

Source: Internet
Author: User
Tags arrays constructor cos hasownproperty

More difficult. Be prepared in your heart ... Deep breath...

1, For/in:
A method of traversing (enumerating) the properties of an object that can loop through the properties we don't know beforehand.
It can enumerate all of the user-defined attributes, but not some predefined properties and methods.
An attribute that cannot be enumerated is usually an inherited property

To delete an object's properties:
Delete book.width;
The property is removed from the object, and after deletion, the property is not enumerated with for/in and is not detected in the width in book.

Another important use of for/in is to use with associative arrays: (If you forget the definition of associative arrays, you can look at the previous chapters.) )
For (stoct in port) {
Value + = Get_value (stoct) * PORT[STOCT];
}

2, common object properties and methods:
1): Constructor properties:
Each object has this attribute, and he references the constructor that initializes the object.
Like what:
var d =new Date (); Use the date () constructor to create an object D;
D.constructor ==date; True//attribute d.constructor references Date;

This property helps to determine the type of an object;
Like what:
We want to determine whether the type of a value is a date type:
If ((typeof o== "Object") && (o.constructor==date)) {
First look at the object and see if the date is referenced
}

The above code can also be written as:
If ((typeof o== "Object") && (o instanceof Date)) {
The instanceof operator to detect the value of the O.constructor property.
}

3, Tostirng () and toLocaleString () methods:
1): Tolocalestirng () returns a localized string for the object.
ToString and tolocalestring generally return the same, but in subclasses, there is a difference:
Like what:
Array, date and number define the toLocaleString () method that returns the localized value.

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 and returns the cosine of the specified angle in radians.
propertyIsEnumerable () and return results are the same as hasOwnProperty ();

4, isPrototypeOf () method:
If the object to which the method belongs is a 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:
Array Direct Quantity:
var es = [];
Complex dot var es = [[1, {x:1, y:2}], [2, {x:3, y:4}]];
There is another way: Use the Array () constructor:
V1: No parameters:
var a = new Array ();
Empty array, and var a =[] equal;

V2: Multiple parameters:
var a = new Array (1,2,3, "tt"); You can see that the direct quantity definition is simpler.

V3:1 a numeric parameter:
var a = new Array (3);
An array with 3 elements, with a value of undefined for each element;

6, Subscript (index) of the array:
Size: 0 <= Subscript < 2 of 32 times side –1;
If it is not in range, JS will say that it is converted to a string as the name of the object property;
Rather than as the subscript of the array;
Like what:
a[-1.2] = "Test"; Equivalent to a[" -1.2"] = "test";
Code Explanation: Create a property named "1.2" instead of defining a new array element.
Current 1/2 page 12 Next read the full text
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.