Common javascript Functions-javascript skills

Source: Internet
Author: User
Tags 0xc0
This article mainly introduces common javascript Functions and summarizes some common functions for your reference. If you need them, refer to the following articles and summarize some common js functions, these include 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

The 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

The 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

The Code is as follows:

Function isNumber (n ){
Return! IsNaN (parseFloat (n) & isFinite (n );
}
Console. log (isNumber ('e345 '));


5. array judgment

The Code is as follows:

Function isArray (obj ){
Return Object. prototype. toString. call (obj) = '[object Array]';
}
Console. log (isArray (arr ));


6. length Comprehension

The Code is as follows:

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

The 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 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 /* C1 */
Do {
C1 = base64DecodeChars [str. charCodeAt (I ++) & 0xff];
} While (I If (c1 =-1) break;
/* C2 */
Do {
C2 = base64DecodeChars [str. charCodeAt (I ++) & 0xff];
} While (I 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 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 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 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 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:

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

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

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: info-contact@alibabacloud.com 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.