1. Built-in objects
- Three types of objects in js: built-in objects, custom objects, and browser objects
- An instance object is an object created by a constructor and then instantiated (new keyword)
- Static objects refer to objects that do not need to be created and are called directly, and public objects that can be called throughout JS. Kind of like a global variable
2. Math
- Math property
console.log(Math.PI); // 3.141592653589793(pi) console.log(Math.E); // 2.718281828459045(base of natural logarithm)
-
Math method
// 1. Absolute value console.log(Math.abs(-1)); // 1 console.log(Math.abs(1)); // 1 // 2. Round up console.log(Math. ceil(2.01)); // 3 console.log(Math.ceil(2.5)); // 3 console.log(Math.ceil(2.99)); // 3 // 3. Round down console.log (Math.floor(2.01)); // 2 console.log(Math.floor(2.5)); // 2 console.log(Math.floor(2.99)); // 2 // 4. Convert to it Number in nearest single-precision float console.log(Math.fround(1.1)); //1.100000023841858 console.log(Math.fround(2 ** 150)); // Infinity console.log(Math.fround("abc")); // NaN console.log(Math.fround(NaN)); // NaN // 5. Take the maximum value and the minimum value console.log(Math.max(3,10,11,58,1,9,6)); // 58 console.log(Math.min(3,10 ,11,58,1,9,6)); // 1 console.log(Math.min(3,10,"abc",58,1,9,6)); // NaN // 6.Number Exponential power of console.log(Math.pow(2,2)); // 4 console.log(Math.pow(4,3)); // 64 // 7. Square root of number console.log(Math.sqrt(16)); // 4 console.log(Math.sqrt(0)); // 0 console.log(Math.sqrt(-16)); // NaN // 8. [0,1) random number console.log(Math.random()); // [0,1) random number console.log(Math.random()*5); // [0,5) Random number console.log(Math.random()*5+1); // Random number for [1,6) console.log(parseInt(Math.random()*5)); // [0,5) random integer of
- case
// Example 1: Customize an object, implement Math.max method function MyMath (){ // Add a method to the object this.getMax= function (){ // Parameters are uncertain, get parameters var max=arguments[0 ] ; for ( var i=0;i<arguments.length;i++ ){ if (max< arguments[i]){ max=arguments[i]; } } return max; }; } // Instance object var mt= new MyMath(); var result=mt.getMax(10,30,50,12,66,1 ); console.log(result); // 66 // Example 2: An array of hexadecimal color values function getColor(){ // Define a string with # for the beginning var str="#" ; / / Define an array, each number and symbol in hexadecimal var arr=["1","2","3","4","5","6","7","8" ,"9","a","b","c","d","e","f" ]; // Because six digits are required, so loop six times for ( var i=0;i <6;i++ ){ // Randomly generate [0,16) integers as subscripts var num=parseInt(Math.random()*16 ) str = str + arr[num]; } return str; } var result=getColor(); console.log(result); // Randomly generate random color values like #1be513
3. Date
- Note that a date object can only be instantiated by calling the Date constructor: calling it as a regular function (ie without the new operator) will return a string, not a date object.
var havenew = new Date (); var nonew = Date (); console.log(havenew); // Thu Apr 26 2018 11:08:13 GMT+0800 (China Standard Time) console.log( typeof havenew); // object console.log(nonew); // Thu Apr 26 2018 11:08:13 GMT+0800 (China Standard Time) console.log( typeof nonew); // string
- Commonly used
var dt1= new Date(); // Thu Apr 26 2018 11:19:27 GMT+0800 (China Standard Time)-----now time var dt2= new Date(2018/4/2); // Thu Jan 01 1970 08:00:00 GMT+0800 (China Standard Time)-----input time var dt3=Date.now(); // 1524712810153--------1970 to present milliseconds (numeric type)
var dt=new Date(); console.log(dt.getFullYear()); // 2018--get the year console.log(dt.getMonth()); // 3--------get the month, the console from 0. log(dt.getDate()); // 26------- get the date console.log(dt.getHours()); // 11------- get the hour console.log(dt .getMinutes()); // 25-----get minutes console.log(dt.getSeconds()); // 57-----get seconds console.log(dt.getDay()); // 4----------Get the day of the week console.log(dt.toString()); // Thu Apr 26 2018 11:25:57 GMT+0800 (China Standard Time)----default console .log(dt.toDateString()); //Thu Apr 26 2018-------Date console.log(dt.toLocaleDateString()); // 2018/4/26 -------Date console.log (dt.toTimeString()); // 11:25:57 GMT+0800 (China Standard Time)------- time console.log(dt.toLocaleDateString()); // 2018/4/26--------- time console.log(dt.valueOf()); // 1524713157751-------Number of milliseconds since 1970
- Format date and time
function getDate(dt){ var year=dt.getFullYear(); // Get the year var month=dt.getMonth(); // Get the month var day=dt.getDate()+1; // Get the day var hour= dt.getHours(); // Get the hour var minute=dt.getMinutes(); // Get the minute var second=dt.getSeconds(); // Get the second // When some numbers are less than 10, add a 0 in front month=month<10?"0"+ month:month; day=day<10?"0"+day:day; hour= hour<10?"0"+ hour: hour; minute=minute<10?"0"+minute:minute; second =second<10?"0"+ second:second; // return the formatted date and time return year+"year"+month+"month"+day+"day"+hour+":"+minute+":"+ second; } var today=new Date(); console.log(getDate(today));
4. String
5. Array