Objects in JS are divided into three types: custom objects, built-in objects, and browser objects

  • Custom objectBuilt-in objectsJs base content, belongs toECMAScript
  • The browser object belongs tojsThe unique

Today I will briefly introduce the built-in objects of JS

The biggest advantage of built-in objects is that they help us develop quickly

Js provides multiple built-in objects: Math, Data, Array, String

1. Math object

1.1 max(),min()Maximum minimum value

  • Math.max()The maximum value of:
  • Math.min(): the minimum

The maximum minimum value must be a number of single values

console.log(Math.max(1.4, -7.10.5.5)); / / 10.5
console.log(Math.min(1.4, -7.10.5.5)); / / - 7
console.log(Math.max('abc')); // NaN
Copy the code

As mentioned earlier in the This section, apply can be used to modify the reference to this based on its nature (the parameters passed in are arrays), so you can use Apply to find the maximum and minimum values of arrays

let arr = [1.4, -7.10.5.5]
console.log(Math.max.apply(Math, arr)); / / 10.5

console.log(Math.min.apply(Math, arr)); / / - 7
Copy the code

1.2 floor()Take down the whole

Math.floor(x) : Round down, which returns the largest integer less than or equal to this number

Two things to remember:

  • The value returned is less than or equal to the original number
  • The value returned is an integer
console.log(Math.floor(1.5));  / / 1
console.log(Math.floor(1.9));  / / 1
console.log(Math.floor(-1.5));  / / - 2
Copy the code

1.3 random()The random number

Math.random() : Returns a random number between [0, 1] by default

Remember: numbers greater than or equal to 0, less than 1, not including 1

console.log(Math.random());  / / 0.7619574163174425
Copy the code

Generate a random true or false

console.log(Math.random() < 0.5 ? true : false);
Copy the code

Returns a random number in an array

var arr = ['First Prize'.'Second prize'.'Third prize'.'Fourth prize']
var random = Math.floor(Math.random() * arr.length);  // Randomly get an integer between 0 and 3
console.log(arr[random]);
Copy the code

1.4 round()rounded

Math.round() is rounded

  • 0.5In particular, it takes the larger one
console.log(Math.round(1.5));  / / - 2
console.log(Math.round(-1.8));  / / - 2
// The larger one is -1
console.log(Math.round(-1.5));  // -1
Copy the code

Case study: Guess

// Returns a random number
function getRandom(min, max) {
    return Math.floor(Math.random() * (max - min) + 1) + min
}
var Random = getRandom(0.10)
var random = prompt('Please enter a number')
var i = 1
while (true) {
    if (i === 3) {
        alert('I'm sorry, you've run out of time.')
        break
    }
    if (random > Random) {
        alert('The input value is large')
        var Random = prompt('Please enter a number')}else if (random < Random) {
            alert('The input is smaller')
            var Random = prompt('Please enter a number')}else {
                alert('Congratulations, you got it.')
            }
    i++;
}
Copy the code

2,Date()The date object

Date() is a constructor that must use new to call our Date object

// Prints the current time
var date = new Date(a)console.log(date); // Sat Apr 10 2021 09:25:04 GMT+0800

// Prints the specified time
var date1 = new Date('the 2021-4-10 9:26:00')
console.log(date1);  // Sat Apr 10 2021 09:26:00 GMT+0800
Copy the code

The Date() object gives us some methods:

The method name instructions
getFullYear() Get the current year
getMonth() Get the current month (0-11)
getDate() Get the current date
getDay() Get the day of the week
getHours() Get the current hour
getMinutes() Get current minute
getSeconds() Get the current second

Get year month day

// No values are written inside the parentheses, and the current time is printed by default
var date = new Date(a);var year = date.getFullYear(); Return the current year
var month = date.getMonth() + 1;  / / in
var dates = date.getDate();  / / number
var day = date.getDay();  / / week
var arr = ['Sunday'.'Monday'.'Tuesday'.'Wednesday'.'Thursday'.'Friday'.'Saturday'];
console.log('Today is:' + year + 'years' + month + 'month' + dates + 'day' + arr[day]);
// Today is: Tuesday 20th October 2020
Copy the code

Gets hours, minutes, and seconds

var h = date.getHours()
h = h < 10 ? '0' + h : h
var m = date.getMinutes()
m = m < 10 ? '0' + m : m
var s = date.getSeconds()
s = s < 10 ? '0' + s : s
console.log(h + ':' + m + ':' + s);  / / 09:51:49
Copy the code

Quick example: Calculate how long it will be before the May Day holiday

function countDown(time) {
    var nowTime = +new Date(a);// Count total milliseconds
    var inputTime = +new Date(time);  // time The total number of milliseconds at that time
    var times = (inputTime - nowTime) / 1000;  // The number of seconds

    var d = parseInt(times / 60 / 60 / 24); // Remaining days
    d = d < 10 ? '0' + d : d;
    var h = parseInt(times / 60 / 60 % 24);
    h = h < 10 ? '0' + h : h;
    var m = parseInt(times / 60 % 60);
    m = m < 10 ? '0' + m : m;
    var s = parseInt(times % 60);
    s = s < 10 ? '0' + s : s;

    return d + 'day' + h + 'hour' + m + 'minutes' + s + '秒';
}

console.log(countDown('2021-5-1 00:00:00')); // 20 days, 14 hours, 04 minutes, 52 seconds
Copy the code

+ New Date() : The total number of milliseconds from our current time to January 1, 1970


3, arrays,

Arrays were described in detail in the previous chapter, so we don’t need to make redundant seconds here

So let’s talk about sorting arrays

Both methods have a common feature: they modify on top of the original array

3.1 reverse()Reverse array

var arr = [10.2.4.5.20.40.50]
console.log(arr.reverse());  // [50, 40, 20, 5, 4, 2, 10]
Copy the code

3.2 sort()Ascending/descending order

sort()grammar

// The default is ascending sort
arr.sort([func])
Copy the code

Try it out:

var arr = [9.8.7.6.5.4.3.2.1]
console.log(arr.sort());  // [1, 2, 3, 4, 5, 6, 7, 8, 9]

var arr = ['George'.'John'.'Thomas'.'James'.'Adrew'.'Martin']
console.log(arr.sort()); // [ 'Adrew', 'George', 'James', 'John', 'Martin', 'Thomas' ]
Copy the code

The needle doesn’t poke, but take a look down here

var arr = [10.8.5.2.20.1]
console.log(arr.sort()); // [1, 10, 2, 20, 5, 8]
Copy the code

Obviously, this sort is wrong

That’s because **sort() starts by the highest digit, so it results in 10 before 2 and 20 before 5

You need to pass in a function as an argument to help sort()

/ / ascending
var arr = [10.8.5.2.20.1]
arr.sort(function(a, b) {
    return a - b
});
console.log(arr);  // [1, 2, 5, 8, 10, 20]

/ / descending
arr.sort(function(a, b) {
    return b - a
});
console.log(arr); // [20, 10, 8, 5, 2, 1]
Copy the code

4. String objects

Look at this article: this is a string object