Some veterans do not necessarily know JavaScript tips _ javascript tips-js tutorials

Source: Internet
Author: User
This article mainly introduces some JavaScript skills that are not necessarily known by some veterans. If you need them, you can refer to some of the less commonly used but powerful JavaScript tips, novice and veteran js developers do not necessarily know.

1. truncation array and array Length

The Code is as follows:

Var arr1 = arr2 = [1, 2, 3];

// Change arr1
Arr1 = []; // arr2 is [1, 2, 3].


You will find that arr1 uses the [] method to clear the value of arr2 without affecting it. If you want to change the value of arr2 after changing the value of arr1, you can do this.

The Code is as follows:


Var arr1 = arr2 = [1, 2, 3];
Arr1.length = 0; // pay attention to this step instead of arr1 = []
Alert (arr2)


Arr2 is also cleared.

2. array Merging

The Code is as follows:


Var arr1 = [1, 2, 3];
Var arr2 = [4, 5, 6];
Var arr3 = arr1.concat (arr2 );
Alert (arr3)


The value of arr3 is changed

The Code is as follows:

[1, 2, 3, 4, 5, 6]


You can also use a simple method, for example

The Code is as follows:

Var arr1 = [1, 2, 3];
Var arr2 = [4, 5, 6];
Array. prototype. push. apply (arr1, arr2 );
Alert (arr1)


Then arr1 becomes 1, 2, 3, 4, 5, 6.

3. browser Feature Detection

Check the code to determine whether your browser is opera

The Code is as follows:

If (window. opera ){
Alert ("It's opera ")
} Else {
Alert ("not opera ")
}


You can do the same.

The Code is as follows:

If ("opera" in window ){
Alert ("It's opera ")
} Else {
Alert ("not opera ")
}

4. Check the object as an array

The Code is as follows:

Var obj = [];
If (Object. prototype. toString. call (obj) = "[object Array]")
Alert ("is an array ");
Else
Alert ("not an array ");


Similarly, you can determine whether the object is a string.

The Code is as follows:

Var obj = "fwe ";
If (Object. prototype. toString. call (obj) = "[object String]")
Alert ("is a string ");
Else
Alert ("not a string ");

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.