Javascript built-in objects
We already know what an object is, how to create an object, and how to use it. now we will introduce some common built-in objects in javascript. (String object, Math object, Number object, Array object, and Date object)
StringObject
Escape characters of the String type.
// Create a String object var string1 = newString ("hello"); var string2 = newString (123); var string3 = newString (1.23); var box = 11; var box = true; alert (typeofbox. toString (); // the string and toString () methods can convert values to strings.
/* The toString () method generally does not need to pass parameters, but when the value is converted to a string, you can pass the hexadecimal parameter */var box = 10; alert (box. toString (); // 10, default decimal output alert (box. toString (2); // 1010, binary output
// CharAt () method and charCodeAt () method -- select a character from the string./* the prompt () function displays a prompt box requesting the user to enter a string, the default value is "helloworld! "*/Var myString = prompt (" enter some text "," hello world! ");/* CharAt () function to obtain a single character at the specified position (last) in the string. index position starts from 0 */var thelastword = myString. charAt (myString. length-1);/* similar to the charAt () method, the charCodeAt () method returns the unicode encoding of a single string at a specific position */
MathObject
/* Trade-off Method * // * Round up * // alert (Math. ceil (25.9); // 26 // alert (Math. ceil (25.5); // 26 // alert (Math. ceil (25.1); // 26/* rounding down * // alert (Math. floor (25.9); // 25 // alert (Math. floor (25.5); // 25 // alert (Math. floor (25.1); // 25/* rounding * // alert (Math. round (25.9); // 26 // alert (Math. round (25.5); // 26 // alert (Math. round (25.1); // 25/* random number method formula: value = Math. floor (Math. random () * Total number + first value) * // alert (Math. floor (Math. random () * 10 + 1); // any number between 1-10/* Transfer range Function * // functionselectFrom (lower, upper) {// var sum = upper-lower + 1; // return Math. floor (Math. random () * sum + lower); //} // for (var I = 0; I <10; I ++) {// document. write (selectFrom (5, 10); // document. write ('');//}
Some other methods:
Vcyh4Kbe1u9h4tcTT4M/release/c/S1rVNYXRoLnRhbih4Kbe1u9h4tcTV/cfQ1rU = "height =" 384 "src =" http://www.bkjia.com/uploads/allimg/150625/0431401419-3.png "width =" 521 "/>
NumberObject
Var box = 250; // a decimal integer alert (box); var box = 070; // an octal integer, with a decimal output of 56 alert (box); var box = 3.80; // floating point alert (box); var box = 12.0; // automatically convert alert (box); var box = 4.12e3; // scientific and technological method alert (box ); var box = 0.000000000412; // scientific and technological method alert (box); var box = 100e10000; // exceeds the range alert (box); var box = 100e10000; alert (isFinite (box); // checks whether the isFinite function is out of the range, and returns false or true
/* NaN, that is, a non-value (Not aNumber) is a special value. This value indicates that no value is returned for the operation of the value to be returned (so that no error is reported ). * /// Var box = 0/0; // NaN // alert (Number. naN) // use Number. naN gets the NaN value // alert (NaN + 1) // the result of any operation with NaN is NaN // alert (NaN = NaN) // NaN is not equal to itself (NaN is not equal to any value)/* The isNaN () function is used to determine whether the value is NaN. After the isNaN function receives a value, it will try to convert it to a value * // alert (isNaN (NaN) // true // alert (isNaN (25)/false
/* There are three functions that convert non-numeric values to numeric values: Number (), parseInt (), and parseFloat (). Number () is applicable to any type, and the last two are used to convert a string to a numeric value. * /// Alert (Number (true); // values of the boolean Type true and false are converted to 1 and 0, respectively. // alert (Number (25 )); // 25, return directly for numeric type // alert (Number (null); // 0, return 0 for null objects // alert (Number (undefined); // NaN, undefined returns NaN // alert (parseInt ('456lee '); // 456, returns the integer part // alert (parseInt ('lee456lee') // NaN, if the first value is not a value, NaN // alert (parseInt ('12lee34lee ') // 12 is returned, starting from the first value, to the last continuous value // alert (parseInt ('12. 34 ') // 12. the decimal point is not a numerical value. // remove alert (parseInt ('') // NaN. If it is null, NaN is returned.
ParseFloat () is used for floating point value conversion. It is used in the same way as parseInt (). It is resolved from the first digit to a non-floating point value. It recognizes only one decimal point and converts the scientific notation to a common value.
ArrayObject
/* Concat () method -- concatenates two arrays to form a new array. */var names = newArray ("Zhang San", "Li Si", "Wang Wu"); var ages = new Array (, 34); var concatArray; concatArray = names. concat (ages );
Figure 1
Figure 2
Figure 2 shows the effect of the merged graph in Figure 1.
/* Slice () method -- obtain some elements of the array */var names = newArray ("Zhang San", "Li Si", "Wang Wu "); // copy the elements of the array to the new array slicearray. The index of the new array is 0. // The retrieved element is "Zhang San". This is a left-closed, right-open, or [) rule. var slicearray = names. slice (1, 2 );
/* Join () method -- converts an array to a string and concatenates these strings into a complete string. the join () method has only one parameter, that is, the separator when array elements are connected to strings * // defines an array object myShoppingvar myShopping = newArray ("apple", "banana ", "orange"); // use the join () method to convert the array elements into a string: "Apple Banana Orange" var list = myShopping. join (""); // document. the write () method prints out the document. write (list );
Other common methods:
Sort () method -- sorts Arrays
Reverse () method -- Reverse the order of elements in the array
DateObject
Obtain the value of the Date object.
Set the value of the Date object
// Create a date object. In the constructor, you can pass parameters to specify the time. If no data is transmitted, the default time is var box = newDate (); alert (box );
// The returned value is 1176307200000 in milliseconds.
Alert (Date. parse ('2014/1/123'); // converts the number of milliseconds to the time var box = newDate (Date. parse ('2014/1/0301'); alert (box); // ThuApr 12 4/12 00:00:00 GMT + 2007 // The number of milliseconds for directly placing the date is also acceptable. Var box = newDate (1176307200000); alert (box); // Data is automatically called in the background by default. parse () var box = newDate ('2014/1/123'); alert (box); // if no Date format parameter is input, return NaNalert (Date. parse (); // Firefox returns Invalid Date (Invalid Date) // Google returns a messy Date // IE returns a NaNvar box = newDate ('msdfkl21123 '); // if the Date format is garbled, InvalidDate (invalid Date) alert (box) is returned; var box = new Date ('may25, 123 '); // Method 2: alert (box); var box = new Date ('thuapr 12 2007 22: 22: 22GMT + 100'); // method 3: alert (box ); // There Are No 32 on March 31, and only 31 are available. You can add + 1 to November 31,. // Google Invalid Date (Invalid Date) // IE October 11, February 1 // Opera is removed from January 1, January 30-1var box = newDate ('january 32,200 7'); alert (box); // The year and month must be input. If this parameter is left blank, there are various errors, negative values, and NaNalert (Date. UTC (); // returns the number of milliseconds alert (Date. UTC (2007,10); // The Coordinated Time in the UTC world, which calculates var box = newDate (Date. UTC (2007, 10, 15, 17, 22, 45, 15); alert (box); // If Date is not added. UTC, then the local time var box = new Date (2007,10, 15, 17, 22, 45, 15); alert (box) is returned ); // The last month is alert (box. getMonth () + 1); // There is an 8-hour gap between the eastern eight regions alert (box. getUTCHours (); All getUTCHous and getHous differ by 8 hours var box = new Date (2007,10, 15, 17, 22, 45, 15); alert (box ); // ThuNov 15 2007 17:22:45 GMT + 0800 alert ('tostring: '+ box. toString (); // Thu Nov 15 2007 17:22:45 GMT + 0800 alert ('tolocalestring: '+ box. toLocaleString (); // 2007-11-15 17:22:45 PS Thu Nov 15 2007 17: 22: 45GMT + 0800 alert ('valueof: '+ box. valueOf (); // 1195118565015
Summary
StringObjectIt provides a lot of convenient methods to use strings, such as calculating the length of a string, searching for a self-string in the string, and selecting a sub-string in the string.
MathObjectAn object automatically created by the javascript execution environment. It provides a large number of mathematical operations and attributes.
NumberObjectConversion between numbers in various hexadecimal formats.
ArrayObjectProvides methods and attributes for operating arrays. For example, obtains the length of an array, sorts an array demonstration, and merges two arrays.
DateObjectProvides methods for saving date, calculating date, and obtaining date and time.