Objects in JavaScript and their methods

Source: Internet
Author: User
Tags array length string format

1. Array objects:

Arrays in JavaScript are a special kind of object. Creating a new array is similar to creating an instance of an object, complete with the new operator and the corresponding array constructor. The constructor of an array is an array (), which can accept multiple parameters and write the following method:

var myarray=New  Array (); var myarray=New Array ("Beijing", "Olympic Games", +,true); // an array with four elements var myarray=New Array (5); // creates an array of the specified number of elements, the value of which is undefined /* The array is created to access each element by subscript, and the subscript starts at 0 */

The jvascript array is sparse, that is, the array element subscript is not stored continuously in memory, such as creating an empty array, adding an element labeled 0 and an element labeled 100, memory will only allocate space for these two elements.

Multidimensional arrays: (for tabular data storage)

 /* creation and access of multidimensional arrays */
var array1=new Array (1,2,3 var Array2=new Array (1,2,3 var Array3=new Array (1,2,3,4 var Array4=new Array (array1,array2,array3); for (var i=0;i<array4.length;++i) { document.write ( for (var j=0;j<4;++j) {Document.writ E (array4[i][j]); }}

Array of methods:
    • Join method and Contact method

/*The join method can use the specified delimiter to stitch the value of an element in an array and return it to the program as a string .*//*The contact can merge two arrays and return to the new array, and the new array is created at the same time as the returned one .*/vararray1=NewArray (All-in-);d Ocument.write (Array1.join ("Q"));//results: 1q2q3vararray1=NewArray (All-in-);varArray2=NewArray ("q", "Z", "E");vararray3=Array1.concat (Array2);d ocument.write (Array3);//Print result 1,2,3,q,z,e
    • Push and Pop methods
// The push () method adds an element at the end of the array and returns the modified array length // the Pop () method deletes 1 elements at the end of the array and returns the element value var array1=new array;d ocument.write ("array length after adding an element:" +array1.push (4));d Ocument.write ( the <br/> array element is: "+Array1";d ocument.write ("<br/> deleted element is:" +array1.pop ());
    • Unshift and Shift methods

// The Unshift method is similar to the push method. is to add an element //shift method to the head of the array, similar to the Pop method, is to delete the element of the array header and return the deleted element

    • Reverse () method and sort ()
// reverse is to invert the original elements of the array and return the inverted array // The sort method is to sort all elements of an array and return sorted arrays, and sort () cannot run without arguments alphabetically, while undefined type elements are sorted at the end of the array. 
    • Slice () method
/* used to intercept part of an array, returns a subarray whose arguments are the starting point element to the endpoint element, and the subarray does not contain an end element. There are only 1 parameters: The representation is truncated from this element until the last element parameter is negative: represents the position of the starting element starting from the end of the array (if 2, which represents the second-to-penultimate element)*/var array1=New Array (1,2,3,8,9,9,5);d ocument.write ("intercepts 3 to 5 elements in an array:" +array1.slice (3,5) + "<br/>"); document.write ("intercept 3 to trailing element in array:" +array1.slice (3) + "<br/>");d Ocument.write ("intercept the last 3 elements in the array:" + Array1.slice ( -3) + "<br/>");

    • Splice method ()

/* Splice is a common method of inserting or deleting an array splice (start, delete quantity); Splice (start, delete quantity, insert element value 1, insert element value 2); */
2. Date objects

A Date object can be used to get datetime, and to create a method by using the method of the object for datetime related operations:

var mynow=New  Date (); /* set an initial value for a Date object in a different format */ var mydata=New Date ("Month day, year, time: minute: Seconds"); var mydate=New date ("Month day, Year"); var mydate=New Date ("year, month, day, time, minute, second")var mydate=New Date ("year, month, Day "); var mydate=New Date ("Number of milliseconds");

Method of the Date object (two formats: local date and Greenwich GMT))

    • Get () Prefix method group:

    • Set Prefix method group

    • Go to String method group

You can convert the date format to a string format as needed.

var mydate=New  date ();d ocument.write ("mydate.todatestring ()); document.write ("mydate.tolocaledatestring ());d Ocument.write ("mydate.tolocalestring ());d Ocument.write (" mydate.tostring ()); document.write ("

3. Arithmetic objects

Math operand math does not need to create an instance and directly accesses its properties and methods, which are called static properties and static methods in object-oriented program design. The Math object's properties are constant values in mathematics, which are constant values that can only be read and not written. The property sheet commonly used by the math object is as follows:

4. String object:

String object has only 2 properties (Length,prototype)

Methods for string objects:

1.HTML format Substitution method

2. How to Operate

5. Function objects

Property:

Length and prototype. The length read-only property that gets the number of arguments for the function; prototype is used to extend the properties and methods of the object.

A function object can be created in JavaScript, which is created when the program is run, so the execution is slower and the method is created as follows:

var myfunction=new function (parameter 1, Parameter 2, ..., parameter n, function body);

MyFunction is a variable that stores a reference to an instance of a function object. A Function object instance does not have a function name, so it is also called an anonymous function.

Method:

    1. Apply (x, y) binds a function to another object's method, the x parameter is the object instance name, the y parameter is the passed argument, and Y can be an array. After the method executes, the this in the body of the function points to the X object instance.
    2. Call (X,y1,y2,..., yn) functions as with apply, the x parameter is the object instance name, and the Y1~yn parameter is the passed argument.
    3. ToString () returns the string form of the function.

Objects in JavaScript and their methods

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.