javaScript commonly used built-in objects

Math object

Math.random()  generate 0---1 Random decimals between, 0 can be generated, 1 cannot be generated  
Math.round()   Get the rounding of a number                          
Math.ceil()    ceiling function,Get the rounded up value of a number         
Math.floor()   floor function,Get the rounded down value of a number             
Math.abs()     Get the absolute value of a number                            
Math.PI        value of pi                                    
Math.pow(2,3)  Get the power of a number     
 
 
console.log(Math.PI)
console.log(Math.round(1.6))
console.log(Math.ceil(0.9))
console.log(Math.floor(3.9))
console.log(Math.pow(2, 10))                    

Date date object

var date = new Date()
Date()is a constructor and requires the keyword new,it's here, date for an instance.

 var date = Date.parse(2009, 10, 25)
      console.log(date)  //This way of writing will count forward one month, because this is calculated by index,
                         //Default is one month ahead
 var date1 = Date.parse('2009,10,25')
      console.log(date1)  //This way of writing will not, because it is in the form of a string.

var   date = new Date()

get date
console.log(date);        //toString() is called internally
date.toDateString();      // foreigners like to use
date.toLocalDateString();
The acquisition time is displayed differently in different browsers
date.toTimeString()
date.toLocalTimeString()

date conversion

var date = new Date(2005,10,1); 
   can accept parameters
   2005,10,1 each part of the date
   "2005-10-1"  string date format
   Represents the date in millisecond form 1128096000000
   var date = Date.parse("2005-10-1");
   Converts each part of a string or 2005, 10, 1 date to the millisecond form of the date, if the format of the string is not the correct format for the time NaN

Get the millisecond form of a date object

//Returns a number, time in milliseconds
var date = Date.now();   //HTML5,IE9+
var date = +new Date();  //When the now method is not supported
var date = new Date();  date.valueOf(); // output date in millisecond form

Get the specified part of the date

method nameeffect
getTime()Returns the number of milliseconds as the result of valueOf()
getMilliseconds()get milliseconds
getSeconds()Returns the number of seconds from 0-59
getMinutes()Returns minutes from 0-59
getHours()Returns the hours from 0-23
getDay()Return day of the week 0 Sunday 6 Saturday
getDate()Returns the day of the current month, the day of the month
getMonth()Returns the month, starting from 0 to 9
getFullYear()Returns a 4-digit year, such as 2018

Array object

Introduce how to use the methods of the Array object

convert array

method nameParaphrase
toString()Convert the array to a string, split each item with
valueOf()Returns the array object itself
 var arr = [10,20,30,40,50,60];
    // console.log(arr.toString()); // output is: 10,20,30,40,50,60,
    // console.log([10, 20, 30, 40, 50, 60,70].toString());
    // console.log([10,20].valueOf()); // output the original array

Manipulate arrays

method nameParaphrase
join()Concatenate the elements in the array into a string, the default is to concatenate. Changes can be made within parentheses.
concat()concatenate two arrays
slice()Intercept a new array from the current array, without affecting the original array, the parameter start starts from 0, and the end starts from 1
splice()Delete or replace some items of the current array, parameters start, deleteCount, options (items to be replaced)
start: start position (subscript) deleteCount: number of deleted elements options: elements to be replaced
 var arr = [10,20,30,40,50,60];/
// var str = arr.join(); // If there are no parameters in the join method, the default is , for connection output
// var str = arr.join('%'); // If there are parameters in the join method, the current parameter is used as the connector
// var str = arr.join(''); // If you don't want to join with any characters, add a vacuum string
// console.log(str);

var strs =['aaa','bbb','ccc'];
var arr1 = arr.concat(strs);
console.log(arr1);

// slice means interception 
var nums = [10,20,30,40,50,60,70];
// var nums1 = nums.slice(1,4); // start: the index to start intercepting end: the index to intercept end can not be obtained
// var nums1 = nums.slice(1); // If there is only one parameter, it means the index value to start intercepting,
//Without the second parameter, it means that it has been intercepted to the end
// console.log(nums1);

// splice intercept, replace
// nums.splice(4); // There is only one parameter, which means to intercept the first 4
// nums.splice(1,4) // When there are two parameters, the first one indicates the index to start deleting data, and the second parameter indicates the number of deletions
// nums.splice(2,3,100,200,300);// Three or more parameters represent replacement
nums.splice(2,3,"aaa","bbb","ccc");// replace
console.log(nums);

Append and delete the preceding item and the following item

method nameParaphrase
push()add to the end of the array
pop()delete the last one in the array
shift()delete the first one in the array
unshift()add to the front of the array
  var nums = [10,20,30,40,50,60];
  // nums[6] = 70;
  // nums[nums.length] = 70;
  // var res = nums.push(70,80,90,100);
  // var res = nums.push(70); // ctrl + d copy the current line to the next line
  // The return value is the new length of the array after adding the array
  // console.log(nums);
  // console.log(res);

    // var res =nums.pop(); // The pop method is used to delete the last item of data in the array
    // console.log(nums);
    // console.log(res); // The return value is the deleted data

   // var res = nums.unshift(100,200); // unshift is used to add data to the front of the array
   //  console.log(nums);
   //  console.log(res); // return value is new length of array

    var res = nums.shift();  //  shift is to delete an item at the front of the array
    console.log(nums);
    console.log(res); // The return value is the item that was deleted

location method

indexOf() finds the position of a certain data in the data

lastIndexOf() finds the position of a certain data in the array starting from the back of the string

method nameParaphrase
indexOf(value[,position])Find the position of a character in a string
lastIndexOf(value[,position])Find the position of a character in a string starting from the back of the array
includes(searchValue[,start])Query whether an array exists from an array, and the second one indicates the starting position

array arrangement

method nameParaphrase
reverse()Invert the array, instead of returning a copy, directly manipulate the array itself
sort()Even the array sort is sorted according to characters, from small to large

check array

instanceof checks whether an array is a subclass or instance of Array

  <script>
    // instanceof is used to check whether it is an instance or a subclass of an array
    var arr = new Array()
    var nums = []

    // instanceof to check whether an array is an instance object of Array
    console.log(arr instanceof Array)
    console.log(nums instanceof Array)

    function fn(){
      console.log(arguments instanceof Array) // false
      // arguments.filter(function(item,index){
      //   console.log(item,index)
      // })
    }
    fn(10,20,30)
  </script>

iterate over an array

method nameParaphrase
filter(function(item,index,arr){})filter
forEach(callback)iterate over the array

Other similar methods are: every() some() map() reduce()

empty array

  1. arr.splice(0,arr.length); // delete all items in the array
  2. arr.length = 0; // length property can be assigned, length is read-only in other languages
  3. arr = []; // this method is recommended
arr.splice(0,arr.length); 
arr.length = 0; // length
arr = [];

String

The string type is an object wrapper type for string values, which can provide us with many useful methods for manipulating strings.

var strObj = new String("hello world");

The object has methods and properties. The property length returns the total number of characters in the current string. Method Character method, string manipulation method, position method, blank removal, case conversion method...

String method

All methods of the string will not modify the string itself (the string is immutable), and a new string will be returned after the operation is completed.

character method.

charAt() //Get the character at the specified position

charCodeAt() //Get the ASCII code of the character at the specified position

str[0] //HTML5, IE8+ support Equivalent to charAt()

concat() //Concatenate strings, equivalent to +, + is more commonly used

slice() //Starting from the start position, intercepting to the end position, the end cannot be obtained

substring() //Start from the start position, intercept to the end position, and the end cannot be obtained

substr() //Start from the start position, intercept length characters, and intercept only one parameter to the end

replace() //replace an element of a string

split() // split string into array

location method

 indexOf() //Returns the position of the specified content in the metastring

 lastIndexOf() //Search from back to front, only find the first match

remove whitespace

 trim() //Only whitespace before and after a string can be removed

Case conversion method

 to(Locale)UpperCase() //Convert uppercase

 to(Locale)LowerCase() //Convert lowercase

Tags: Javascript Vue.js Front-end

Posted by webbyboy on Thu, 14 Jul 2022 01:48:03 +0530