Javascript learning notes (2) JavaScript core objects and Arrays

Source: Internet
Author: User
Tags hasownproperty

An object is a composite data type.
The simplest way to create an object is your Javascript Code Contains the Direct Volume of objects. You can also create an object by using the new operator.
VaR empty ={}; // an object with no properties
VaR point = {X: 0, Y: 0 };
VaR Circle = {X: Point. X, Y: Point. Y + 1, radius: 2 };
VaR Homer = {
"Name": "Homer Simpson ",
"Age": 34,
"Married": True,
"Occupation": "plant operator ",
'E-mail ': homer@example.com
};
VaR A = new array (); // create an empty array
VaR d = new date (); // create an object representing the current date and time
VaR r = new Regexp ("JavaScript", "I"); // create a pattern-matching object
After creating an object, we can use the "." operator to create new attributes, reference existing attributes, and set attribute values in the object.
VaR book = new object (); // create an object
Book. Title = "javascript: the definitive guide ";
Book. Chapter1 = new object (); // as an object attribute, nested object
Book. chapter1.title = "Introduction to JavaScript ";
Book. chapter1.pages = 11;
Book. Chapter2 = {Title: "lexical structure", pages: 6 };
Alert ("outline:" + book. Title + "\ n \ t" +
"Chapter 1" + book. chapter1.title + "\ n \ t" +
"Chapter 2" + book. chapter2.title); // read some attributes from the object.
In the preceding example, you can create a value by assigning it to a new property of the object.
The javascript statement mentioned that the for/in statement can be used to traverse the attributes and methods of objects.
Use the in operator to check whether a property exists. For example:
If ("X" in O) o. x = 1; // If yes, set the attribute value to 1.
If the write is as follows: O. X = undefined // attribute x exists, but there is no value. I often write: If (O. X! = Undefined) o. x = 1;
In addition ,! = Often replaced! = .! ==And === the difference is that undefined and null can be omitted when no partition is needed, such as: If (O. dosomething) O. dosomething ();
Delete attribute: delete book. Chapter2;
Objects associated with Arrays: object. Property and object ["property"] are equivalent.
Attributes and methods of common objects
Constructor attributes: var d = new date (); D. constructor = date; // true
Since the constructor defines the class of an object, the attribute constructor helps to determine the type of the given object. for example, you can use the following code to determine the type of an unknown object: If (typeof o = "object") & (O. constructor = Date), you can also use the instanceof OPERATOR: If (typeof o = "object") & (O instanceof date ))
Tostring () method: returns a string that represents the type or value of the object that calls it. when JavaScript needs to convert an object to a string, it calls the tostring () method of this object. for example, when you use "+" to connect a character and an object, or pass an object to alert () or document. when the write () method is used, tostring () is called ().
The default tostring () method does not provide much information. for example, the following code can only obtain the string "[object]": var S = {X: 1, Y: 1 }. tostring (); therefore, many Classes define their own tostring () methods. For example, when an array is converted into a string, a list of array elements is obtained, each element is converted to a string. When a function is converted to a string, Source code . Chapter 9 describes in detail.
Tolocalestring () method: returns the localized string representation of the object. the default tolocalestring () method defined by the oject class does not have any localization. The returned result is exactly the same as that returned by the tostring () method, but the subclass of the object class may define its own tolocalestring () method.
Valueof () method: similar to tostring (), it needs to be called when JavaScript needs to convert an object to an original type (usually a number) other than a string.
Hasownproperty method: if the object partially defines a non-inherited attribute and the attribute name is specified by the actual parameter of a string, this method returns true. Otherwise, it returns false. For example:
VaR o = {};
O. hasownproperty ("UNDEF"); // false: This attribute is not defined.
O. hasownproperty ("tostring"); // false: tostring is an inherited property.
Math. hasownproperty ("Cos"); // true: the math object has the COs attribute.
Propertyisenumerable () method: if an object defines an attribute, the attribute name is specified by the actual parameter of a string, and the attribute can be enumerated cyclically using for/in, this method returns true. Otherwise, false is returned. for example:
VaR o = {X: 1 };
O. propertyisenumerable ("X"); // true: property exists and is enumerable
O. propertyisenumerable ("Y"); // false: property doesn't exist
O. propertyisenumerable ("valueof"); // false: property is inherited
Isprototypeof () method: If the called object is the prototype object of the object specified by the actual parameter, true is returned for this method; otherwise, false is returned. The purpose of this method is similar to the constructor attribute of the object. For example:
VaR o = {}
Object. Prototype. isprototypeof (o); // true: O. constructor = Object
Object. isprototypeof (o); // false
O. isprototypeof (object. Prototype); // false
Function. Prototype. isprototypeof (object); // true: object. constructor = Function
Array method:
Join (): All elements in an array can be converted into strings and connected. You can specify an optional string to separate the elements in the result string. if no separator string is specified, you can use commas to separate elements.
Reverse (): returns the inverted array element order and returns the inverted array. it performs this operation on the original array, that is, it does not create a new array that resorts elements, but rearranges the array elements in an existing array.
Sort (): sorts the array elements in the original array and returns the sorted array. if sort () is called without passing the parameter to it, it sorts the array elements alphabetically (if necessary, the elements can be converted to strings for comparison)
Concat (): Creates and returns an array. this array contains the elements of the original array that calls Concat (), followed by the Concat () parameter. if some of the parameters are arrays, they are expanded and their elements are added to the returned array. note that Concat () cannot recursively expand an element as an array.
Slice (): The returned array is a segment.
Splice (): a general method for inserting or deleting array elements. It modifies the array on the original array.
Push () and POP (): This allows us to use arrays as we use stacks. the push () method can append one or more new elements to the end of the array. then return the new length of the array. the method POP () is the opposite.
Unshift () and shift (): they are very similar to push () and POP (), except that they insert and delete elements in the header of the array, instead of inserting or deleting elements at the end.
Tostring () and tolocalestring (): note that there are no square brackets or other delimiters around the array value in the output result.
Array appending: indexof () and lastindexof (), foreach (), map (), filter ()

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.