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 ");