1.Function objects
function is a very special object, special in that the object is like a method in Java, can run, can pass parameters.
There are three ways to define a function object:
1.function Fun1 (A, b) {
alert (A+B);
}
2.var fun2 = new Function ("A", "B", "Alert (a+b);"); /The last parameter represents the function body
3.var fun3 = function (A, b) {
alert (A+B);
}
Things to note when calling a function:
1.js function in the call, compared to Java, only need to fill in the function name, no matter how to fill the parameters can be called. (If the number of arguments does not match)
There are no overloads in 2.js, but you can simulate overloaded functionality with built-in object arguments, which represents the actual argument list at runtime of the parameter.
2.1 use its. Length property to get the number of actual arguments.
2.2 Use Arguments[n] to obtain a specific parameter.
3. Return of the function.
Return keyword is also used for returning functions in 3.1 JS
3.2 Like Java, return can also be used as the end function of the run.
The function of the 4.void () operator is to intercept the return value of the function so that it returns undefined.
2. Three major packaging types of learning
Three packaging objects String number (understanding) Boolean (understanding)
Attribute: The string number Boolean in JS is a pseudo-class. You can use the methods and properties in the three wrapper objects directly.
Use of string
varstr =NewString ("Hello"); //methods are divided into three categories //1. No use (little in development), help us to generate HTML code method.Alert (Str.anchor ("abc"))); Alert (Str.big ()); Alert (Str.sup ()); //2. The importantAlert (Str.charat (1));//e Returns the character at the specified positionAlert (Str.charcodeat (1));//returns the ASC code value for the corresponding characterAlert (Str.concat ("World"));//connection string Hello worldAlert (Str.indexof ("L"));//2 Retrieving StringsAlert (Str.lastindexof ("L"));//3 the string is searched forward from the back. Alert (str.substring (0, 2));//He does not contain the last character of a parameter's corner markAlert (Str.slice (-3,-1));//Hel supports negative numbers from right to left. //3. Use with regular objectsmactch Search Split replace</script>
3.typeof and instanceof
typeof is used to determine which primitive type belongs to (five primitive types: number,string,undefined,boolean,null)
Instanceof is used to determine which type the object belongs to.
4.Global objects (built-in objects)
Use reason: When the Get method is submitted, if there is Chinese, it will be transferred to other codes such as:? NAME=%E7%A2 ==> is because the HTTP protocol has problems in transmitting Chinese. So the form Transfer Chinese browser will automatically transcode, so use this object's method to Transcode.
encodeURI (URI) to encode Chinese
decodeURI (Encodeduri) decoding Chinese
encodeURIComponent (uricomponent) encodes some symbols in addition to the Chinese encoding. Use this method to encode when we are passing parameters that use some special symbols.
decodeURIComponent (uricomponent)
Escape (s) has been eliminated from the method.
Unescape (s)
The two method scenarios are different, as follows:
// ---------encodeURI decodeURI------------------------ var str = "http://www.baidu.com?name= wisdom"; var str2 = encodeURI (str); alert (str2); Alert (decodeURI (STR2)); // -------------encodeuricomponentdecodeuricomponent-------------------- var str_3 = "Http://www.baidu.com?name= Zhang: Three/" var str_4 = encodeuricomponent (str_3); alert (str_4); Alert (decodeURIComponent (str_4));
5.Math objects
Math is a built-in object that differs from global in that it requires the "Math" to be typed when called. Prefix.
Method slightly
6.Array objects
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >//Array Object //How to create 1 varARR1 = [n/a];//3 //Create Method 2 All parameters in the constructor function as elements of the array. varARR2 =NewArray (All-in-); //alert (arr2.length); //Create mode 3 if the array constructor, only one parameter is filled and is a number. Then this parameter is the length of the specified array, not the elements of the array. varARR3 =NewArray (3);//creates an empty array of length 3. //Properties in Array //the Length property ==> the lengths of the array. //alert (arr3.length); //Traversal of Array /*For (var i = 0; i< arr2.length; i++) {alert (arr2[i]); } */ //the method in the array. //concat two arrays added. //The Join method returns a string whose contents are concatenated with each element in the array (by the given parameter). //Alert (Arr1.join ("-")); //---------------Pop method push method------------------ //use these two methods to simulate the data structure of a stack. (Advanced post-exit) /*alert (Arr1.pop ());//3 alert (arr1.length);//2*/ //Alert (Arr1.push (4));//4 //------------------------Reverse to invert the array---------- /*Arr1.reverse (); Alert (Arr1.join ()); */ //---------------------Sort Sorts--------------------------- //Note: The sort method is sorted by default by string comparison rules. /*var arr4 = [8,2,3,9,1,100]; Arr4.sort (_ABC); Alert (Arr4.join ()); */ //Comparator 1 functionABC (A, b) {if(A >b) { return1; }Else if(A = =b) { return0; }Else{ return-1; } } //Comparator 2 function_abc (A, b) {returnA-b; } //the properties of arrays in JS //The attributes of an array in Java, which specifies what type of array, can only be loaded in what type. There is only one type. //The array attribute in JS 1:js can be any type, without any restrictions. //The array attribute in JS is 2:js, and the length is changed with the subscript. How long it will take. varARR5 = [' abc ', 123, 1.14,true,NULL, Undefined,NewString (' 1213 '),NewFunction (' A ', ' B ', ' Alert (a+b) ')]; /*alert (arr5.length);//8 arr5[10] = "hahaha"; alert (arr5.length); Alert (arr5[9]);//undefined*/ //-----------------------Object-oriented category, this phenomenon should not be studied.-----------------------------------arr5[' abc '] = 100; alert (arr5.length);//8Alert (arr5[' abc ')]); </script>7.Date objects
1.new date () Gets the current time
2.getFullYear () Get year
3.getMonth () Get month note January results for 0
4.getHours () hours
5.getDate () Date
6.getMinutes () min
7.getSeconds () Get seconds
8.getTime () Gets the millisecond value.
9. toLocaleString () Gets the local time format string.
//Example one: Print out the current date format as follows. //June 18, 2014 15:40:30 Wednesday functiongetcurrentdate () {//1. Create a Date object varDate =NewDate ();//The current time is not filled in with any parameters . //2. Get the current year varYear =date.getfullyear (); //3. Get the current month JS in the month is from 0 to one. varmonth = Date.getmonth () +1; //4. Get the current day varDay =date.getdate (); //5. Get the current hour varhour =date.gethours (); //6. Get the current minute varMin =date.getminutes (); //7. Get Current seconds varSEC =date.getseconds (); //8. Get the current week varWeek = Date.getday ();//no Getweek . //June 18, 2014 15:40:30 Wednesday returnyear+ "Year" +changenum (month) + "monthly" +day+ "Day" +hour+ ":" +min+ ":" +sec+ "" +Parseweek (week); } //alert (Getcurrentdate ()); //solutions for automatically completing two-digit numbers functionchangenum (num) {if(Num < 10){ return"0" +num; }Else{ returnnum; } } //convert digital 0~6 to Sunday to Saturday functionParseweek (week) {vararr = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; //0 1 2 3 ... .... returnArr[week]; } //Example two: Get the current green time in milliseconds and convert it to the current date varDate =NewDate (); varTime = Date.gettime ()-10000000000; //Date construction method, can be filled in green millisecond number varDate2 =NewDate (time); //Alert (date2.tolocalestring ());
8.REGEXP Objects (Regular)
This object is used during form validation to verify that the user-populated string conforms to the rule.
Create a regular object mode 1 parameter 1 regular expression parameter 2 Validate mode g global/i ignore case/M multiple line match. Parameter 2 general fill g can be.
The first letter of the username must be in English, except for the first digit and only the English number and _. The shortest length can not be less than 6 bits maximum not more than 12 bits
<script type= "Text/javascript" >//RegExp Object //This object is used during form validation to verify that the user-populated string conforms to the rule. //Create a regular object mode 1 parameter 1 Regular expression parameter 2 Verify mode g global/i ignores case. Parameter 2 general fill G can be. //The first letter of the username must be in English, except for the first and only English numerals and _. The shortest length can not be less than 6 bits maximum not more than 12 bits //----------------------------How to create 1 /*var reg1 = new RegExp ("^[a-za-z][a-za-z0-9_]{5,11}$", "G"); Verify string var str = "bc123"; Alert (Reg1.test (str));//TRUE//----------------------------creation Method 2/fill in regular expression/match mode; var reg2 =/^[a-za-z][a-za-z0-9_]{5,11}$/g; Alert (Reg2.test (str));//True*/ //-------------------------------The method of the regular object------------------- //The test method ==> tests whether a string is a compound regular rule. The return value is true and false. //-------------------------4 methods that are combined with a regular in a String------------------. //macth Search Split replace varstr = "Hello World"; //alert (Str.match (/o/g));//Find the contents of the compound regular in the string. //alert (Str.search (/h/g));//0 Find the content location in the string that matches the regular expression //alert (Str.split (/o/g));//The string is cut according to a regular expression. Returns an array;Alert (Str.replace (/o/g, "s"));//Hells Wsrld the string as a regular replacement.</script>
JavaScripts Learning Diary--ecmascript