[Web front-end basics | JS basics] Built-in objects

1-Built-in objects

1.1 Built-in objects

​ There are three types of objects in JavaScript:

  1. Custom object
  2. Built-in objects The
    first two objects are the basic content of JS and belong to ECMAScript;
  3. Browser object
    Browser object is unique to JS

JS API explains that built-in objects refer to some of the objects that come with the JS language. These objects are for developers to use and provide some commonly used or the most basic and necessary functional properties and methods). The biggest advantage of built-in objects is to help us quickly Development

​ JavaScript provides multiple built-in objects: Math, Date, Array, String, etc.

1.2 Check documents

Finding documents: To learn the use of a built-in object, as long as you learn the use of its common members, we can learn by looking up the document, and you can query through MDN/W3C.
The Mozilla Developer Network (MDN) provides information about Open Web technologies, including HTML, CSS, and APIs for the World Wide Web and HTML5 applications.

MDN Web


1
2

1.3 Math object

The Math object is not a constructor and does not need to be called with new. It has the properties and methods of mathematical constants and functions. Math-related operations (absolute value, rounding, maximum value, etc.) can use the members in Math.

Property, method name Features
Math.PI PI
Math.floor() Round down
Math.ceil() Improvement arrangement
Math.round() The rounded version is rounded to the nearest integer. Note -3.5 The result is -3
Math.abs() Absolute value
Math.max()/Math.min() Find the maximum and minimum
Math.random() Get a random value in the range [0,1), return a floating point number

​ Note: The above method must be used with parentheses, and then you can continue to check the document if you don’t understand, the method of use is unusually detailed

<script>
    console.log(Math.max(1, 2, 3, 4, 5, 35, 42, 34, 24)); //42
    console.log(Math.min(53452, 1, 34, 53, 45, 435, 251)); //1
    console.log(Math.PI); //3.141592653589793
    console.log(Math.floor(2.9)); //2
    console.log(Math.ceil(2.1)); //3
    console.log(Math.round(-1.1)); //-1
</script>

Acquiring random integer within the specified range :

function getRandom(min, max) {
    
    
  return Math.floor(Math.random() * (max - min + 1)) + min; 
}

1.4 Date Object

Date is a constructor, so you need to instantiate it before you can use its specific methods and properties.
Date instance is used to process date and time

  • Use Date to instantiate a date object

    • Get the current time must be instantiated:
    var now = new Date();
    
    • Get the date object at the specified time
    var future = new Date('2019/5/1');
    

    note:

○ If no parameters are passed in, the date object obtained is the date object corresponding to the current time

○ Parameters can be passed in

1: You can pass in a string

3
2: You can pass in several numbers, the month starts from 0

4

  • Use the methods and properties of Date instances
Obtain Method var date = new Date();
Get the year var year = date.getFullYear();
Get month var month = date.getMonth() + 1;
Get date var day = date.getDate();
Get the week var week = date.getDay();
When getting var hour = date.getHours();
Get points var minute = date.getMinutes();
Get seconds var second = date.getSeconds();
  • Get the total number of millimeters through the Date instance

    • The meaning of total milliseconds

      ​ Based on the number of milliseconds since January 1, 1970 (Coordinated Universal Time)

    • Get the total number of milliseconds

      // 实例化Date对象
      var now = new Date();
      // 1. 用于获取对象的原始值
      console.log(date.valueOf())	
      console.log(date.getTime())	
      // 2. 简单写可以这么做
      var now = + new Date();			
      // 3. HTML5中提供的方法,有兼容性问题
      var now = Date.now();
      

❤ Time difference

Guess you like

Origin blog.csdn.net/qq_43490212/article/details/111483921