JavaScript-basic syntax (3)

Source: Internet
Author: User

JavaScript-basic syntax (3)

 <Script type = text/javascript> // String object: var str1 = xx var str2 = new String (d); var str = aasdasd; outln (len = + str. length); outln (str. bold (); // equivalentStrOutln (str. fontcolor (red); // equivalent to str outln (str. link (; // hyperlink outln (str. substr (); // captures three outln (str. substring (1, 3); // the header does not contain the end </script>

Custom functions:

1. Define a method to remove spaces at both ends of a string
function trim(x){             var i = 0,j = x.length-1;             for(;i< x.length, x.charAt(i)==' ',j>=0, x.charAt(j)==' ';i++,j--);             return x.substring(i,j+1);         }         var str =   a s d asd  ;        alert(str);         alert(-+trim(str)+-);

Prototype attribute of String: prototype (emphasis)

The bold () method of the string can be directly called by the string object, and the trim method above is also used to operate the string. How can we add the trim method to the string object, called directly by an object,

The prototype of String is String. prototype.
Then we can use a prototype property of this string to complete
Prototype: A description of the object. If a new function is added to the description
The object will have this function.
Prototype can get the original object of the string (prototype can extend the function of the object)

Add the custom trim method to str.
Function trim (x) {var I = 0, j = x. length-1; for (; I <x. length, x. charAt (I) = '', j> = 0, x. charAt (j) = ''; I ++, j --); return x. substring (I, j + 1);} var str = a s d asd; String. prototype. sss = 250; // Add an sss attribute named sss = 199 to the string prototype; // All String objects with sss are 199 // The first string. prototype. trim = trim; alert (str. trim (str); // type 2, anonymous String. prototype. trim = function () {var I = 0, j = this. length-1; for (; I <this. length, this. charAt (I) = '', j> = 0, this. charAt (j) = ''; I ++, j --); return this. substring (I, j + 1);} alert (str. trim ();/* encapsulate it as a separate file during development. Import the file */

Prototype exercise:
1. Add a string-to-array function to the string
2. added the string inversion function.
Var str = abcd; String. prototype. toCharArray = function () {var ch = []; for (var I = 0; I
Array object: ArrayArray method:
Var arr = [asd, cd, 1, 2, ds]; var arr1 = [d, cvd]; var newarr = arr. concat (arr. concat (aa, arr1); // connect aa to the arr array, and then link the arr1 array to/* outln (newarr); outln (newarr. join (); // default comma outln (newarr. join (); outln (newarr. pop (); // remove the last element of the array and return outln (newarr. push (zzz); // Add a new element to the array and return the new length. The element can also be an array of two elements in // JS. var s = [[], [], [], []; outln (arr. reverse (); // reverse outln (arr. shift (); // remove the first element from the array and return outln (arr); outln (arr. slice (); // return a segment in the array. * // the header does not include the end. If one of the headers is negative, the value is + length, do not assign a value to the end greater than the header // outln (arr. sort (); // returns an Array object that has been sorted // outln (arr. splice (, 9527, asd, xc); // 0 indicates the start position, 3 indicates the deletion of three elements, and 4 elements are added. // remove one or more elements, you can add a new element to the position where the element is removed, and return the removed element // arr. unshift (12233); // Insert the specified element into the starting position of the array and return the array // outln (arr); // unshift and pop to implement the data structure: queue // unshift and shift: implement data structure: Stack

Array exercise custom function
// Custom Array Function
Array. prototype. getMax = function () {var tep = 0; for (var I = 0; I
Tep) tep = I;} return this [tep];} // rewrites the Array. prototype. toString = function () {return [+ this. join () +];}

Common Js object-Date object

Purpose: Enable the basic storage and obtain the date and time.
Var obj = null; obj = new Date (); obj = new Date (dateva); // dateva: January 1, 1970 .... Obj = new Date (year, month, date [, [minutes [, [seconds [, MS]);

Method Demonstration:
Var date = new Date (); // document. writeln (date); // default method // document. writeln (date. toLocaleString () +); // current date and time // outln (date. toLocaleDateString (); // date // outln (date. getFullYear () +/+ (date. getMonth () + 1) +/+ date. getDate (); var week = date. getDay (); function getWeek (num) {var week = [Sunday, Monday, Tuesday, Wednesday, Thursday, Saturday]; return week [num];} // outln (week is: + getWeek (week); var date2 = new Date (); var time = date2.getTime (); // obtain the millisecond value // outln (time); // 1417177204024: // convert the millisecond value to the Date object Date (); 2. setTime (); var date3 = new Date (time); // convert the Date object to the string toLocaleString (), toLocaleDateString () // convert a string to a Date string in the specified format-> millisecond value-> Date object var date_str = 11/28/2014; var time_mi = Date. parse (date_str); // The millisecond difference between the string and 1970.1.1, but it must be: Month/day/year var date4 = new Date (time_mi); outln (date4.toLocaleString ());

Special Js statement:

To simplify the writing of object calling content, you can use the special statement with in Js to complete

Witn (object ){
You can directly use the content of the specified object in this region without the need for the object.

var date = new Date();         with(date){            //var year = date.getFullYear();             var year = getFullYear();         }         outln(year);

Common Js object Math-basic functions:

Note that the methods in this object are static and you can call Math directly.
Var num1 = Math. ceil (12.35); // greater than or equal to the minimum integer var num2 = Math. floor (12.34); // less than or equal to the maximum integer var num3 = Math. round (12.56); // rounding outln (num1 = + num1); outln (num2 = + num2); outln (num3 = + num3); for (var I = 0; I <10; I ++) {// var num = Math. random () * 10 + 1; // the pseudo-random number is generated. // var num = Math. floor (Math. random () * 10 + 1); var num = parseInt (Math. random () * 10 + 1); // parseInt is a Global method, and the Global Object outln (num );}

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