Basic functions of string
Out: Encapsulated document.write ();
Many ways to repeat with Java
<script type= "Text/javascript" src= "Out.js" > </script> <script type= "Text/javascript" > / /string object: var str1 = "xx" var str2 = new String ("D"); var str = "AASDASD"; Outln ("len =" +str.length); Outln (Str.bold ());//equivalent to <b>str</b> outln (Str.fontcolor ("red");//equivalent to <font-color= "red" >str </font> outln (Str.link ("http://www.baidu.com"));//Hyperlink outln (Str.substr (1,3)),//intercept 3 from subscript 1 Outln (str.substring (1,3))//with head not including tail </script>
Custom Features:
1. Define a method to remove whitespace 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 properties for string: Prototype (emphasis)
The Bold () method for a string can be called directly by a string object, and the Trim method above is also used to manipulate the string, how the trim method is added to the string object, directly called by the object,
The prototype of string is String.prototype
Then we can use a prototype property of the string to do
Prototype: is a description of the object, if a new feature is added in the description
Then the object will have this function
And prototype can get the original object of the string (prototype can extend the function of the object)
Add a 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;//Adds an SSS property named SSS = 199 to the string prototype; Then a random string object has SSS, and both are 199 //The first String.prototype.trim = trim; Alert (Str.trim (str)); The second, 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 ()); /* Package as a separate file at development time, import */
Prototyping Exercises:
1. The ability to add a string variable group to a string
2. Add string inversion function
var str = "ABCD"; String.prototype.toCharArray = function () { var ch = []; for (var i = 0;i<this.length;i++) Ch[i] = This.charat (i); return ch; } String.prototype.rev = function () { var ch = this.tochararray (); var low = 0,high = This.length-1; Function Swap (STR,I,J) {//functions defined in the function, Java is not allowed, similar to the Java internal class var t = str[i]; Str[i] = str[j]; STR[J] = t; } while (Low
Array objects: array
Array method:
var arr = ["ASD", "CD", "N", "DS"]; var arr1 = ["D", "CVD"]; var newarr = Arr.concat (Arr.concat ("AA", arr1));//Connect "AA" as an array of arr, and then arr1 array link/*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 a two-bit array in the arrays//js, Var s = [[],[],[],[]]; Outln (Arr.reverse ());//Invert outln (Arr.shift ());//Move the first element in the array and return to OUTLN (arr); Outln (Arr.slice (1,3));//Returns a paragraph in the array, *//with the head not containing the tail, the tail and tail of a negative, +length, the end is greater than the head does not do assignment//outln (Arr.sort ());//Returns an already sorted AR Ray Object//outln (Arr.splice (0,3,250,9527, "ASD", "XC")),//0 is the start position, 3 is to delete 3 elements, add 4 elements//remove one or more elements, and you can add a new element at the location of the removed element, return Removes the element//arr.unshift ("12233");//Inserts the specified element at the beginning of the array and returns the array//outln (ARR); Unshift and Pop, you can implement data structures: Queue//unshift and Shift: Implementing Data structures: Stacks
Array exercises Custom Features
Customizing the array feature
Array.prototype.getMax = function () { var tep = 0; for (var i = 0;i<this.length;i++) { if (this[i]>tep) tep = i; } return this[tep];} The replication of the array Array.prototype.toString = function () { return "[" +this.join () + "]";}
JS Common Object-date Object
Function: Date and time when basic storage is enabled and taken
var obj = Null;obj = new Date (); obj = new Date (Dateva);//dateva: Distance 1970 .... obj = new Date (Year,month,date[,[minutes[,[seconds[,ms]]]);
Method Demo:
var date = new Date (); Document.writeln (date);//default mode//document.writeln (date.tolocalestring () + "<br/>");//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", "Friday", "Saturday"]; return Week[num]; }//Outln ("Week is:" +getweek (week)); var date2 = new Date (); var time = Date2.gettime ();//Get Millisecond value//outln (time),//1417177204024:20 point 20//Convert millisecond value to date object 1.new (); 2.setTime (); var date3 = new Date (time); Date object is converted to string tolocalestring (), tolocaledatestring ()//String to date object with the specified format date string--millisecond value--Date object Var date_s TR = "11/28/2014"; var time_mi = Date.parse (DATE_STR);//The string is less than the millisecond of 1970.1.1, but must be: month/day/year var date4 = new Date (TIME_MI); Outln (Date4.tolocalestring ());
JS-specific statement: with
To simplify the writing of object invocation content, you can use the unique statement with in JS to complete
Witn (object) {
The contents of the specified object can be used directly within the region, and no objects are required.
}
var date = new Date (); With (date) { //var year = Date.getfullyear (); var year = getFullYear (); } OUTLN (year);
JS Common Object Math Object-Basic features:
Note that the methods in this object are static, and the direct math call can
var num1 = Math.ceil (12.35);//the smallest integer var num2 = Math.floor (12.34) greater than or equal to the specified argument, or//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;//This generates a pseudo-random number //var num = Math.floor (math.random () *10 +1); can be var num = parseint (math.random () *10+1);//parseint is the global method, the Globals Object outln (num); }
javascript-Basic Grammar (3)