Summarize common javascript Functions and summarize javascript Functions
This article summarizes some common js functions, including data operations, character replacement operations, date and encryption/Decryption operation functions, and some simple verification functions. For your reference. I believe it will help you.
1. typeof, constructor, and instanceof determine the Array
Copy codeThe Code is as follows: var arr = [1, 5, 6, 9, 8, 5, 4];
// Var arr = new Array (1, 5, 6, 9, 8, 5, 4 );
Console. log (typeof arr); // object
Console. log (arr. constructor (); // []
Console. log (arr instanceof Array); // true
2. function Self-call
Copy codeThe Code is as follows: (function (a, B ){
Console. log (a + B); // 30 self-called
}) (10, 20); 3. Remove spaces on both sides
Function trim (str ){
Return str. replace (/^ s + | s + $/g ,'');
}
Console. log ('woshi sss'); // woshi sss
Console. log (trim ('woshi sss'); // woshi sss
4. digit judgment
Copy codeThe Code is as follows: function isNumber (n ){
Return! IsNaN (parseFloat (n) & isFinite (n );
}
Console. log (isNumber ('e345 '));
5. array judgment
Copy codeThe Code is as follows: function isArray (obj ){
Return Object. prototype. toString. call (obj) = '[object Array]';
}
Console. log (isArray (arr ));
6. length Comprehension
Copy codeCode: var myArray = [12,222,100 0];
MyArray. length = 0; // myArray will become an empty array []. ----- length is a readable and writable attribute
7. base64_encode encryption and decryption
Copy codeThe Code is as follows: function base64encode (str ){
Var out, I, len, base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + /";
Var c1, c2, c3;
Len = str. length;
I = 0;
Out = "";
While (I <len ){
C1 = str. charCodeAt (I ++) & 0xff;
If (I = len ){
Out + = base64EncodeChars. charAt (c1> 2 );
Out + = base64EncodeChars. charAt (c1 & 0x3) <4 );
Out + = "= ";
Break;
}
C2 = str. charCodeAt (I ++ );
If (I = len ){
Out + = base64EncodeChars. charAt (c1> 2 );
Out + = base64EncodeChars. charAt (c1 & 0x3) <4) | (c2 & 0xF0)> 4 ));
Out + = base64EncodeChars. charAt (c2 & 0xF) <2 );
Out + = "= ";
Break;
}
C3 = str. charCodeAt (I ++ );
Out + = base64EncodeChars. charAt (c1> 2 );
Out + = base64EncodeChars. charAt (c1 & 0x3) <4) | (c2 & 0xF0)> 4 ));
Out + = base64EncodeChars. charAt (c2 & 0xF) <2) | (c3 & 0xC0)> 6 ));
Out + = base64EncodeChars. charAt (c3 & 0x3F );
}
Return out;
}
Function base64decode (str ){
Var c1, c2, c3, c4, base64DecodeChars = new Array (-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-,-1,-1,-, 55,, 60, 61,-1,-1,-1,-1,-1,-1,-1,-, 9, 10, 13,14, 15,16, 17,18, 19,20, 22, 25,-1,-1,-1,-1,-1,-1,-, 27,28, 29,30, 31,32, 33,34, 35,36, 37,38, 39,40, 41,42, 43,44, 45,46, 47,48, 49,50, 51,-1,-1,-1,-1,-1 );
Var I, len, out;
Len = str. length;
I = 0;
Out = "";
While (I <len ){
/* C1 */
Do {
C1 = base64DecodeChars [str. charCodeAt (I ++) & 0xff];
} While (I <len & c1 =-1 );
If (c1 =-1) break;
/* C2 */
Do {
C2 = base64DecodeChars [str. charCodeAt (I ++) & 0xff];
} While (I <len & c2 =-1 );
If (c2 =-1) break;
Out + = String. fromCharCode (c1 <2) | (c2 & 0x30)> 4 ));
/* C3 */
Do {
C3 = str. charCodeAt (I ++) & 0xff;
If (c3 = 61) return out;
C3 = base64DecodeChars [c3];
} While (I <len & c3 =-1 );
If (c3 =-1) break;
Out + = String. fromCharCode (c2 & 0XF) <4) | (c3 & 0x3C)> 2 ));
/* C4 */
Do {
C4 = str. charCodeAt (I ++) & 0xff;
If (c4 = 61) return out;
C4 = base64DecodeChars [c4];
} While (I <len & c4 =-1 );
If (c4 =-1) break;
Out + = String. fromCharCode (c3 & 0x03) <6) | c4 );
}
Return out;
}
Function utf16to8 (str ){
Var out, I, len, c;
Out = "";
Len = str. length;
For (I = 0; I <len; I ++ ){
C = str. charCodeAt (I );
If (c> = 0x0001) & (c <= 0x007F )){
Out + = str. charAt (I );
} Else if (c> 0x07FF ){
Out + = String. fromCharCode (0xE0 | (c> 12) & 0x0F ));
Out + = String. fromCharCode (0x80 | (c> 6) & 0x3F ));
Out + = String. fromCharCode (0x80 | (c> 0) & 0x3F ));
} Else {
Out + = String. fromCharCode (0xC0 | (c> 6) & 0x1F ));
Out + = String. fromCharCode (0x80 | (c> 0) & 0x3F ));
}
}
Return out;
}
Function utf8to16 (str ){
Var out, I, len, c;
Var char2, char3;
Out = "";
Len = str. length;
I = 0;
While (I <len ){
C = str. charCodeAt (I ++ );
Switch (c> 4 ){
Case 0:
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
Case 6:
Case 7:
// 0 xxxxxxx
Out + = str. charAt (I-1 );
Break;
Case 12:
Case 13:
// 110x xxxx 10xx xxxx
Char2 = str. charCodeAt (I ++ );
Out + = String. fromCharCode (c & 0x1F) <6) | (char2 & 0x3F ));
Break;
Case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
Char2 = str. charCodeAt (I ++ );
Char3 = str. charCodeAt (I ++ );
Out + = String. fromCharCode (c & 0x0F) <12) | (char2 & 0x3F) <6) | (char3 & 0x3F) <0 ));
Break;
}
}
Return out;
}
8. Date and Time
GetDay function: returns the day of the week. The value ranges from 0 ~ 6. 0 indicates Sunday, 1 indicates Monday,..., 6 indicates Saturday. Example:
Copy codeThe Code is as follows: function DateDemo ()
{
Var d, day, x, s = "Today is :";
Var x = new Array ("Sunday", "Monday", "Tuesday ");
Var x = x. concat ("Wednesday", "Thursday", "Friday ");
Var x = x. concat ("Saturday ");
D = new Date ();
Day = d. getDay ();
Return (s + = x [day]);
}
(3) getHouse function: returns the "Hour" part of the date, with a value of 0 ~ 23. Example.
Copy codeThe Code is as follows: function TimeDemo ()
{
Var d, s = "The current local time is :";
Var c = ":";
D = new Date ();
S + = d. getHours () + c;
S + = d. getMinutes () + c;
S + = d. getSeconds () + c;
S + = d. getMilliseconds ();
Return (s );
}
(4) getMinutes function: returns the "Minute" part of the date. The value ranges from 0 ~ 59. See the previous example.
(5) getMonth function: returns the "month" part of the date, with a value of 0 ~ 11. Where 0 represents January, 2 represents March,..., 11 represents December. See the previous example.
(6) getSeconds function: returns the "second" part of the date. The value ranges from 0 ~ 59. See the previous example.
(7) getTime function: return the system time.
I hope this article will help you design javascript programs.
What are common javascript Functions?
SetInterval setTimeout, alert (), etc. Basically, you define the function yourfunction () {function body}
Common javascript Functions and Methods
Doc.51windows.net/..ir.htm