Article directory
Common Objects in Js
Dater Date object
// New object var date = new Date(); // Days of the week date.getDay(); // Days of the month date.getDate(); // Return month 0-11 date.getMonth(); // Return to the difference between 1900 and the present date.getYear(); // What year is it now to return? date.getFullYear(); // Return to local time date.toLocaleString();
Mathematical function object
// Getting the Random Range of Random Numbers is [0-1] var ran = Math.random() * 1000; // Rounding up Math.ceil() // Rounding down Math.floor() // Get 4-digit authentication code Math.random() * 9000 + 1000;
Sting object
// Get the content of subscript 2 str.charAt(2); // Get subscript str.indexOf("a"); // String interception str.substr(2,4); str.substr(1); str.substring(2,4); // Segmentation by specified characters str.split("-");
Global object
Global objects can be used directly
// Execute strings as js code var b = "var c = 1 + 1"; eval(b); // Check whether a value is a number isNaN("123a");
Array objects
Array declaration
Method 1:
var arr1 = new Array();
Mode two:
var arr2 = new Array(2); // 2 is the length of the array. At this point, the output array will show that the two elements of the array are empty.
Mode three:
var arr3= new Array(1, "Hello", new String(), true);
Mode four
// This is the same usage as java var arr4 = ["jssd", 123, new Date(), false];
Arrays need to be used
The subscripts of arrays in js can be discontinuous and empty if there is no value
function demo() { var arr=[]; arr[0] = "Fur"; // Automatic expansion to length = 1; arr[1] = true; // Automatic expansion to length=2 arr[6] = 12; // Automatically expand to length=7, empty is the array that has no previous assignment }
Expansion of arrays
function demo() { var arr=["jssd", 123, new Date(), false]; // Expanding array, empty unassigned arr.length = 10; // Reducing the excess of the array will be discarded arr.length = 2; }
Traversal of arrays
One way
function demo() { var arr = ["jssd", 123, new Date(), "Test it."]; for (var i = 0; i < arr1.length; i++) { alert(arr1[i]); } }
Mode two
var arr = ["jssd", 123, new Date(), "Test it."]; for (let x in arr1) { // x is the subscript of an array console.log(arr[x]); }
Mode three
var arr = ["jssd", 123, new Date(), "Test it."] for (let x of arr1) { console.log(x); }
Common methods in arrays
// Add one or more elements to the end of the array and return the length of the result arr.push("We"); // Delete and return the last element of the array arr.pop(); //Add one or more elements to the beginning of the array and return a new length arr.unshift("Hello"); // A first element arr.shift(); // splice function arr.splice(1, 2); // Delete from subscript 1, delete 2 elements arr.splice(1, 0, "you", "good"); // Starting from subscript 1, delete 0 elements and add two elements. The function of adding elements at specified positions is realized.