There are three types of objects in JavaScript: custom objects, built-in objects, and browser objects
Built-in objects are some objects that come with the js language for developers to use, and provide some commonly used or the most basic and necessary functions (properties and methods). The biggest advantage is to help rapid development
Examples of built-in objects: Math, Date, Array, String
Table of contents
1. Check the document MDN/W3C
The Mozilla Developer Network (MDN) provides information on Open Web technologies (Open Web), including HTML, CSS, and APIs for the World Wide Web and HTML5 applications
MDN:http://developer.mozilla.org/zh-CN
Two, Math object
2.1 Math is a built-in object, which has some mathematical constant properties and mathematical function methods.
Math is not a constructor, so you don't need new to call, but directly use the properties and methods inside
Math.PI //pi
Math.floor() // round down, get smaller
Math.ceil() // round up, get bigger
Math.round() //The rounded version is aggressive. Note that the result of -3.5 is -3. .5 special, take the bigger one
Math.abs() //Absolute value
Math.max() / Math.min // Find the maximum and minimum values
2.2 Write myMath object
var myMath = {
PI : 3.1415926,
max : function(){
var max = arguments[0];
for(i = 0;i < arguments.length;i++){
if(arguments[i]>max){
max = arguments[i];
}
}
return max;
},
min : function(){
var min = arguments[0];
for(i = 0;i < arguments.length;i++){
if(arguments[i]<min){
min = arguments[i];
}
}
return min;
}
}
console.log(myMath.PI);
console.log(myMath.max(9,101,99,100));
console.log(myMath.min(9,101,99,100));
2.3 Math.random()
Math.random(): Random number method, returns a random floating point decimal [0,1)
method does not follow parameters
① Returns a random number [min,max] between two integer endpoints
Method: Math.floor(Math.random()*(max-min+1))+min
//返回包括端点两个数之间的随机整数
function getRandom(min,max){
return Math.floor(Math.random()*(max-min+1))+min;
}
console.log(getRandom(1,10));
Three, the date object Date
3.1 Creating objects
It is used to deal with date and time. It is a constructor. You must use new to construct objects and instantiate them to use
//Date对象,Date是一个构造函数,必须使用new来创建对象
var arr = new Array();//创建一个数组对象
var obj = new Object();//创建了一个对象实例
var date = new Date();//
//使用Date,如果没有参数,返回当前时间
console.log(date);//Sat Apr 02 2022 16:40:26 GMT+0800 (中国标准时间)
When the parameter writes the time, it will return the time in the parameter, but the return time (****, **, **) is a bit special
var date = new Date();
console.log(date);//Sat Apr 02 2022 16:40:26 GMT+0800 (中国标准时间)
var date1 = new Date(2022,4,2);
console.log(date1);//Mon May 02 2022 00:00:00 GMT+0800 (中国标准时间)
var date2 = new Date('2022-4-2');
console.log(date2);//Sat Apr 02 2022 00:00:00 GMT+0800 (中国标准时间)
3.2 Method:
// format date hour minute second
getHours() //current time
getMinutes() // minutes
getSeconds()//seconds
getDay() // Get the day of the week
getDate() //Get the date of the day
getMonth() // Get the current month (0-11)
getFullYear() //Get the current year
3.3 Timestamp
Get the total milliseconds of Date, not the milliseconds of the current time, but the number of milliseconds since January 1, 1970.
1. Through valueOf() getTime()
2. Commonly used simple writing +new Date() return
var date3 = +new Date();
console.log('总的毫秒数'+date3);
3. HS newly added to get the total number of milliseconds Date.now()
console.log(Date.now())
4. Countdown case
function countDown(time){
var nowTime = +new Date();
var inputTime = +new Date(time);
var times = (inputTime - nowTime)/1000; //剩余时间总的秒数
var d = parseInt(times / 60 / 60 / 24);
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 + '天' + h + '时' + m + '分' + s + '秒';
}
console.log(countDown('2022-4-6 13:00:00'));