JS study notes ten-Date-Math-packaging

One, Date function object

  1. Use date object to represent a time in Js
  2. If you directly use the constructor to create a Date object, it will be encapsulated as the current code execution time.
var d=new Date();//封装,这个代码什么时候执行,显示时间就是什么时候
console.log(d);
  1. To create a specified time object, you need to pass a string representing the time as a parameter in the constructor.
var d2=new Date("01/24/2021 11:30:46");
console.log(d2);
  1. Date format: month/day/year hour: minute: second

Second, the Date method

5. getDate()

:Get the current date object is a few days

6. getDay()

: Get the current date object is the day of the week, it will return a value of 0-6, 0 means Sunday

7. getMonth()

Get the month of the current time object, return a value of 0-11, 0 means January

8. getFullYear()

Get the year of the current object

9.getTime()

Get the timestamp of the current object. The time error refers to the number of milliseconds from 0:00:00 on January 1, 1970, Greenwich Mean Time to the current time (1 second = 1000 milliseconds). The bottom layer of the computer uses timestamps when saving time

10. Get the current timestamp

Use timestamps to test the performance of code execution

var start=Date.now();
var end=Date.now();

Three, Math () object

Math is different from other objects. It is not a constructor. It belongs to a tool class without creating objects. It encapsulates properties and methods related to mathematical operations.

1.abs()

Can be used to calculate the absolute value of a number.

2.Math.ceil()

A number can be rounded up, and the decimal place is rounded up as long as it has a value.

3.Math.floor()

Round down, the decimal part will be rounded off

4.Math.round()

Round a number

5.Math.random()

Can be used to generate a random number between 0-1
Generate a random number between 0-x: Math.round(Math.random()*x)
Generate a random number between xy:Math.round(Math.random()*(y-x)+x)

6.Math.pow(x,y) returns x to the power of y

7.Math.sqrt() is used to root a number

Four, packaging

Three packaging classes are provided for us in js, through which the data types of basic data types can be converted into objects.

1. String(): You can convert a basic data type string into a String object

1. At the bottom, the string is stored in the form of a character array.
2. length attribute : can be used to get the length of the string.
3. The charAt() method can get the character at the specified position in the string, and get the specified character according to the index. The original character remains unchanged, and the return value is the specified character.
Insert picture description here
4. The charCodeAt() method returns the number of the specified character in unicode encoding.
5. The formCharCode() method can get the characters according to the character encoding
. 6. The concat() method : connect two or more strings, the job is the same.
7. The indexof() method : This method can retrieve whether a string contains the specified content. If the content is in the string, the index of its first occurrence will be returned. If the specified content is not found, -1 is returned. You can specify a second parameter to specify where to start the search.

Find h from the sixth positionInsert picture description here

8. lastIndexOf() method : This method is the same as indexOf. The difference is that indexOf is searched from front to back, while lastIndexOf is searched from back to front. You can also specify the method to start the search.

  1. Slice (start position index, end position index) method : you can intercept the specified content from the string, will not affect the original string, but return the intercepted content. You can also pass a negative number, and the negative number will be calculated later.

  2. The substring (start position index, end position index) method : can be used to intercept a string, similar to slice(), except that substring() cannot pass negative numbers. If you pass a negative value, 0 is used by default. If two parameters, the second is less than the first, the position of the parameters will be adjusted automatically.

  3. Split() method: You can split a string into an array.
    Parameters: A string is required as a parameter, and the array will be split according to the string.
    If you pass an empty string as a parameter, each character will be split into an element in the array.
    The output result is an array object Insert picture description here
    12. toUpperCase() method : Convert a string to uppercase and return it.
    13. toLowerCase() method : Convert a string to lowercase and return.

2. Number(): The basic data type number can be converted into a Number object.

3. Boolean(): You can convert Boolean values ​​of basic data types into Boolean objects.

创建一个Number类型的对象
var num=new Number(3);
  1. But note: we will not use objects of basic data types in actual applications. If you use objects of basic data types, some unpredictable results may be brought about when doing some comparisons.
  2. Methods and properties can be added to objects, but not to basic data types.
    When we call properties and methods on values ​​of some basic data types, the browser will temporarily use the wrapper class to convert them into objects, and then call the properties and methods of the object.
    After the call, it is converted to a basic data type.
var s=123;
s=s.toString();

Guess you like

Origin blog.csdn.net/weixin_45636381/article/details/113079960