当前位置:网站首页>JS built in object
JS built in object
2022-06-12 12:16:00 【Yolo. H】
Math object :
// 1. Absolute value method
console.log(Math.abs(1)); // 1
console.log(Math.abs(-1)); // 1
console.log(Math.abs('-1')); // Implicit conversion Will put the string type -1 Convert to digital
console.log(Math.abs('pink')); // NaN
// 2. Three rounding methods
// (1) Math.floor() The floor Rounding down Take the minimum value
console.log(Math.floor(1.1)); // 1
console.log(Math.floor(1.9)); // 1
// (2) Math.ceil() ceil The ceiling Rounding up Take the maximum value
console.log(Math.ceil(1.1)); // 2
console.log(Math.ceil(1.9)); // 2
// (3) Math.round() rounding All other numbers are rounded , however .5 special It's going big
console.log(Math.round(1.1)); // 1
console.log(Math.round(1.5)); // 2
console.log(Math.round(1.9)); // 2
console.log(Math.round(-1.1)); // -1
console.log(Math.round(-1.5)); // The result is -1
//3. Maximum
Math.max()
console.log(Math.max(1, 99, 3)); // 99
console.log(Math.max(-1, -10)); // -1
//4. minimum value
Math.min()
console.log(Math.max(1, 99, 'pink teacher ')); // NaN
console.log(Math.max()); // -Infinity
//5.PI
Math.PI
console.log(Math.PI);
//6. random number
// 1.Math Object random number method random() Returns a random decimal 0 =< x < 1
// 2. There are no parameters in this method
// 3. Code validation
console.log(Math.random());
// 4. We want to get a random integer between two numbers also Include this 2 It's an integer
// 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));
// 5. Random roll call
var arr = [' Zhang San ', ' Zhang Sanfeng ', ' Zhang San is crazy ', ' Li Si ', ' Li Sisi ', 'pink teacher '];
console.log(arr[getRandom(0, arr.length - 1)]);
Date object
//Date() The date object is a constructor , You have to use new To call the create date object
//Date The object is based on 1970 year 1 month 1 Japan ( World standard time ) Milliseconds from
//1. Use Date No parameters , Returns the current time of the current system
var date = new Date();
console.log(date)
//2. Returns the current year
console.log(date.getFullYear());
//3. Returns the current month 1 Months , want +1
console.log(date.getMonth());
console.log(date.getMonth() + 1);
//4. Return the number
console.log(date.getDate());
//5. Back to the week , Sunday - Saturday 0-6
console.log(date.getDay());
//6. return
console.log(date.getHours());
//7. Return points
console.log(date.getMinutes());
//8. Return seconds
console.log(date.getSeconds());
//9. Return distance 1970 year 1 month 1 How many milliseconds have passed ( Time stamp , Never repeat )
console.log(date.valueOf());
console.log(date.getTime());
var dat = +new Date(); //+new Date() The most common way to write
console.log(dat)
console.log(Date.now()) //H5 Newly added , The simplest
//10. Common writing of parameters
//- Digital
var date1 = new Date(2019, 10, 1)
console.log(date1); //Fri Nov 01 2019 00:00:00 GMT+0800 ( China standard time )
//- String type ( The main )
var date2 = new Date('2019-10-1 8:8:8')
console.log(date2); //Tue Oct 01 2019 08:08:08 GMT+0800 ( China standard time )
// Format in minutes and seconds
function getTimer() {
var time = new Date();
var h = time.getHours();
h = h < 10 ? '0' + h : h;
var m = time.getMinutes();
m = m < 10 ? '0' + m : m;
var s = time.getSeconds();
s = s < 10 ? '0' + s : s;
return h + ':' + m + ':' + s;
}
// format 2019 year 5 month 1 Japan Wednesday
function getYMD() {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var dates = date.getDate();
var arr = [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '];
var day = date.getDay();
return (' It's today :' + year + ' year ' + month + ' month ' + dates + ' Japan ' + arr[day]);
}
// The countdown effect
function countDown(time) {
var nowTime = +new Date(); // Returns the total number of milliseconds of the current time
var inputTime = +new Date(time); // Returns the total number of milliseconds of user input time
var times = (inputTime - nowTime) / 1000; // times Is the total number of seconds remaining
var d = parseInt(times / 60 / 60 / 24); // God
d = d < 10 ? '0' + d : d;
var h = parseInt(times / 60 / 60 % 24); // when
h = h < 10 ? '0' + h : h;
var m = parseInt(times / 60 % 60); // branch
m = m < 10 ? '0' + m : m;
var s = parseInt(times % 60); // Current seconds
s = s < 10 ? '0' + s : s;
return d + ' God ' + h + ' when ' + m + ' branch ' + s + ' second ';
}
console.log(countDown('2019-5-1 18:00:00'));
var date = new Date();
console.log(date);
Array objects
// Check if it's an array
// 1.instanceof
var arr = []
console.log(arr instanceof Array); //true
//2. Array.isArray( Parameters ) H5 Newly added ,ie9 Above version support
console.log(Array.isArray(arr)); //true
// Add or remove array elements
//1.push() Add one or more array elements to the end of the array Returns the length of the new array
var arr = [1, 2, 3]
// arr.push(4, 'Pink')
console.log(arr.push(4, 'Pink')); //5
console.log(arr); //(5) [1, 2, 3, 4, "Pink"]
//2.unshift('red') Add a new element to the array header Returns the length of the new array
console.log(arr.unshift(0, 'youyouo')); //7
console.log(arr); //[0, "youyouo", 1, 2, 3, 4, "Pink"]
// Remove elements
//1.pop Delete the last element Returns the element to be deleted
console.log(arr.pop())
console.log(arr);
//2.shift() Delete first element Returns the element to be deleted
console.log(arr.shift())
console.log(arr);
// Array sorting
//1. Flip array
var arr = ['red', 'pink', 'green']
arr.reverse()
console.log(arr);
//2. Array sorting ( Bubble sort )
var arr = [3, 9, 7, 8]
arr.sort()
console.log(arr); //[3, 7, 8, 9]
var arr = [3, 43, 2, 18]
arr.sort()
console.log(arr); //[18, 2, 3, 43] Here is a comparison of
var arr = [3, 43, 2, 18]
arr.sort(function (a, b) {
return a - b // Ascending [2, 3, 18, 43]
// return b - a // Descending [43, 18, 3, 2]
})
console.log(arr);
// Array index
// indexOf Returns the index number of the first element of the array , When the element cannot be found , return -1
var arr = ['red', 'pink', 'green', 'red']
console.log(arr.indexOf('red')); //0
// lastIndexOf
console.log(arr.lastIndexOf('red')); //3
// Array weight removal
function unique(arr) {
var newArr = []
for (var i = 0; i < arr.length; i++) {
if (i === arr.indexOf(arr[i])) {
newArr.push(arr[i])
}
}
return newArr
}
var re = unique([2, 3, 5, 67, 2, 3, 4, 5, 7])
console.log(re);
// Array to string
//1.toString()
var arr = ['red', 'pink', 'green', 'red']
console.log(arr.toString()); //red,pink,green,red
//2. Separator
var arr = ['red', 'pink', 'green', 'red']
console.log(arr.join()); //red,pink,green,red
console.log(arr.join('-')); //red-pink-green-red
console.log(arr.join('/'));//red/pink/green/red

Basic packaging type
// Basic packaging type
var str = 'andy';
console.log(str.length);
object Only then Properties and methods Complex data types Properties and methods
Why simple data types have length Attribute? ? Basic packaging type : Is to put Simple data type Packaging has become Complex data type
Three basic packaging types :String,Number,Boolean
// so :
// Basic packaging type
var str = 'andy';
console.log(str.length);
// (1) Wrapping simple data types into complex data types
var temp = new String('andy');
// (2) Put the value of the temporary variable to str
str = temp;
// (3) Destroy this temporary variable
temp = null;
String object
// The immutability of strings
// It means that the values in it are immutable
// Although it seems that the content can be changed , But the address has changed , A new memory space has been opened up in memory .
// So don't splice a lot of strings
// Return position according to character
// String name .indexOf(' Characters to find ', Starting position ) Return if not found -1
var str = ' The spring breeze of reform blows the earth , Spring is coming '
console.log(str.indexOf(' In the spring ')); //2 first ' In the spring '
console.log(str.indexOf(' In the spring ', 3)); //8
// String name .lastIndexOf(' Characters to find ') Look backwards , Returns the first matching character
console.log(str.lastIndexOf(' In the spring ')); //8
// Find string "abcoefoxyozzopp" All in o Where and how often
// The core algorithm : Find the first one first o Position of appearance
// then as long as indexOf The result returned is not -1 Just keep looking back
// because indexOf You can only find the first , So the following search , It must be the current index plus 1, To keep looking for
var str = "oabcoefoxyozzopp";
var index = str.indexOf('o');
var num = 0;
// console.log(index);
while (index !== -1) {
console.log(index);
num++;
index = str.indexOf('o', index + 1);
}
console.log('o The number of times is : ' + num);
// Returns the character according to the position
//1.chatAt(index)
// Traverse all the characters
for (var i = 0; i < str.length; i++) {
console.log(str.charAt(i));
}
// 2. charCodeAt(index) Returns the character of the corresponding index number ASCII value Purpose : Judge that the user pressed that key
console.log(str.charCodeAt(0)); // 97
// 3. str[index] H5 Newly added
console.log(str[0]); // a
// Judging a string 'abcoefoxyozzopp' The most frequently used character in , And count the times .
// o.a = 1
// o.b = 1
// o.c = 1
// o.o = 4
// The core algorithm : utilize charAt() Traversing this string
// Store every character in the object , If the object does not have this property , for 1, If it exists +1
// Traversing objects , Get the maximum value and the character
var str = 'abcoefoxyozzopp';
var o = {
};
for (var i = 0; i < str.length; i++) {
var chars = str.charAt(i); // chars yes Every character in a string
if (o[chars]) {
// o[chars] What you get is the attribute value
o[chars]++;
} else {
o[chars] = 1;
}
}
console.log(o);
// 2. Traversing objects
var max = 0;
var ch = '';
for (var k in o) {
// k Get is Property name
// o[k] What you get is the attribute value
if (o[k] > max) {
max = o[k];
ch = k;
}
}
console.log(max);
console.log(' The most common characters are ' + ch);
// String splicing concat(' character string 1',' character string 2'....)
var str = 'andy';
console.log(str.concat('red'));
// String interception substr(' Intercept start position ', ' Intercept a few characters ');
var str1 = ' The spring breeze of reform is sweeping the ground ';
console.log(str1.substr(2, 2)); // first 2 It's the index number 2 From the first few the second 2 Is to take a few
// Replace character replace(' Replaced characters ', ' Replace with the new character ') It will only replace the first character
var str = 'andyandy';
console.log(str.replace('a', 'b'));
// There's a string 'abcoefoxyozzopp' Ask for all the inside o Replace with *
var str1 = 'abcoefoxyozzopp';
while (str1.indexOf('o') !== -1) {
str1 = str1.replace('o', '*');
}
console.log(str1);
// Character to array split(' Separator ') We learned about join Convert an array to a string
var str2 = 'red, pink, blue';
console.log(str2.split(','));
var str3 = 'red&pink&blue';
console.log(str3.split('&'));
// Convert a capital
toUpperCase()
// Convert lowercase
toLowerCase()

边栏推荐
- 用vector保存对象时保存指针的优点, 以及reserve的使用
- object. Defineproperty basic usage
- 宏编译 预处理头 WIN32_LEAN_AND_MEAN
- Longest string without duplicate characters (leetcode 3)
- 恭喜Splashtop 荣获2022年 IT Europa “年度垂直应用解决方案”奖
- #ifndef#define#endif防止头文件重复包含, 你不是真的懂
- Open source project - (erp+ Hotel + e-commerce) background management system
- 寻找两个有序数组的中位数(LeetCode 4)
- Difference between Definition and Declaration
- Clone with cloneNode to solve the id problem / methods deep copy and shallow copy to modify the ID
猜你喜欢
随机推荐
Cookie和Session
Implementation principle of kotlin extension function
【Leetcode】416. Split equal sum subset
Cookies and sessions
NDT registration principle
B. Wall painting (C language)
AND THE BIT GOES DOWN: REVISITING THE QUANTIZATION OF NEURAL NETWORKS
Create servlet project
ARP protocol data processing process of neighbor subsystem
Quic wire layout specification - packet types and formats | quic protocol standard Chinese translation (2) package type and format
银行布局元宇宙:数字藏品、数字员工成主赛道!
Imx6 uboot add lvds1 display
for in 与Object.keys()的区别
KDD2022 | 边信息增强图Transformer
Backtracking, eight queens
PDSCH related
【Leetcode】199. Right view of binary tree
Chapter VI data type (V)
Longest string without duplicate characters (leetcode 3)
Reasons for SSL introduction and encryption steps









