_javascript skills of JavaScript common function induction and collation

Source: Internet
Author: User
Tags 0xc0 trim

This paper summarizes some JS common functions, including data manipulation, character replacement operation, date and decryption operation function, and some simple validation functions. Convenient for everyone to consult the reference. I believe it will be helpful to you.

The judgment of 1.typeof, constructor and instanceof in the log group

Copy Code code 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 from call

Copy Code code as follows:
(function (A, b) {
Console.log (A + b);//30 from calling
}) (10, 20); 3. Remove both sides of the space
function Trim (str) {
Return Str.replace (/^s+|s+$/g, "");
}
Console.log (' Woshi sss ');//Woshi SSS
Console.log (Trim (' Woshi sss '));//woshi SSS

4. Digital Judgment

Copy Code code as follows:
function Isnumber (n) {
Return!isnan (parsefloat (n)) && isfinite (n);
}
Console.log (Isnumber (' e345 '));

5. Array judgment

Copy Code code as follows:
function IsArray (obj) {
return Object.prototype.toString.call (obj) = = ' [Object Array] ';
}
Console.log (IsArray (arr));

6.length of understanding

Copy Code code as follows:
var myarray = [12, 222, 1000];
myarray.length = 0; MyArray will become an empty array [].-----length is a readable writable property

7, Base64_encode encryption, decryption

Copy Code code 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&GT;&GT;2);
Out+=base64encodechars.charat ((c1&0x3) <<4);
out+= "= =";
Break
}
C2=str.charcodeat (i++);
if (I==len) {
Out+=base64encodechars.charat (C1&GT;&GT;2);
Out+=base64encodechars.charat ((c1&0x3) <<4) | ( (c2&0xf0) >>4));
Out+=base64encodechars.charat ((C2&AMP;0XF) <<2);
out+= "=";
Break
}
C3=str.charcodeat (i++);
Out+=base64encodechars.charat (C1&GT;&GT;2);
Out+=base64encodechars.charat ((c1&0x3) <<4) | ( (c2&0xf0) >>4));
Out+=base64encodechars.charat ((c2&0xf) <<2) | ( (C3&AMP;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,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,- 1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,- 1,26,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:
0xxxxxxx
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 is 0~6, where 0 means Sunday, 1 means Monday, ..., 6 means Saturday. Cases:

Copy Code code 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" section of the date, and the value is 0~23. Cases.

Copy Code code as follows:
function Timedemo ()
{
var d, s = "The current Local:";
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 "Minutes" section of the date, and the value is 0~59. See the above example.
(5) GetMonth function: Returns the "month" section of the date, and the value is 0~11. Of these, 0 said January, 2 means March, ..., 11 means December. See the previous example.
(6) Getseconds function: Returns the "Seconds" section of the date, and the value is 0~59. See the previous example.
(7) GetTime function: Returns the system time.

I hope this article will help you with your JavaScript programming.

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