Below are the learning notes about JavaScript written in my W3C School tutorial. I have personally tested each method. I have added my own understanding of the purpose and parameter list of each method after I personally practiced it, and the explanation is quite detailed. Now record it for reference:
JavaScript Array class:
There are two ways to create js arrays:
var arr = []; or
var arr = new Array();
In (), you can specify the length or not. It doesn’t matter if you specify it.
Because the array length in js is variable
concat(arr1,arr2,arr3......arrx): js array combination, return the merged new array, at least one array needs to be passed, or multiple arrays can be passed.
vararr1=newArray(3);arr1[0]="George";arr1[1]="John";arr1[2]="Thomas";arr1[3]="Jeery";vararr2=newArray(3);arr2[0]="James";arr2[1]="Adrew";arr2[2]="Martin";vararr3=newArray(3);arr3[0]="Java";arr3[1]="C#";arr3[2]="PHP";vararr4=arr1.concat(arr2,arr3);alert(arr4);
join(): splicing array elements into a string according to the specified separator. The default separator is an English comma.
vararr=newArray(3)arr[0]="George";arr[1]="John";arr[2]="Thomas";arr[3]="Jeery";arr.join(".");sort(fn): Array sorting, by default, it is arranged in ascending order of the ASC code of English letters, for example, apple is ranked in front of orange, actually
sort can also receive a parameter, which is a bit similar to the comparator in java.
That is to say, if you do not want to sort by the default comparison rules, you must provide a comparison function, which has two parameters a and b,
If the return value is less than 0, a is ahead of b
If the return value is greater than 0, b is ahead of a
If the return value is equal to 0, the positions of a and b will not change
vararr=newArray(6);arr[0]=5;arr[1]=23;arr[2]=4;arr[3]=18;arr[4]=88;arr[5]=10;arr.sort(sortNumber);functionsortNumber(a,b){returna-b;}pop(): Delete the last element of the array, reduce the length of the array by 1, and return the value of the element it deletes.
If the array is already empty, pop() does not change the array and returns the undefined value.
vararr=newArray(6);arr[0]=5;arr[1]=23;arr[2]=4;arr[3]=18;arr[4]=88;arr[5]=10;vara=arr.pop();alert(a);for(varxinarr){alert(arr[x]);}push(n1,n2,n3,...nx): Add one or more elements to the end of the array and return the length of the added array.
Note that this method operates on the original array object and will not create a copy. This method can receive multiple parameters,
At least one parameter must be passed
vararr=newArray(6);arr[0]=5;arr[1]=23;arr[2]=4;arr[3]=18;arr[4]=88;arr[5]=10;varlen=arr.push(44,80);alert(len);for(varxinarr){alert(arr[x]);}reverse(): reverse the order of elements in the array, that is, if the original array elements are 1, 2, 3, 4, 5, after calling reverse(),
The element order is 5, 4, 3, 2, 1. Note that this method directly operates on the original array object and will not create a copy.
vararr=[3,5,11,6,90,0];arr.reverse();for(varxinarr){alert(arr[x]);}shift(): Delete the first element of the array and return the element it deleted
If the array is already empty, shift() does not change the array and returns the undefined value
Note that this method directly operates on the original array and will not create a copy object.
vararr=[3,5,11,6,90,0];vara=arr.shift();alert(a);for(varxinarr){alert(arr[x]);}slice(start, end): used to intercept the array element between start and end and save it into a new array to return
Note that this method will not modify the original array, and will create a copy of the array object.
If end is not specified, it means starting directly to the end of the array.
If start or end is negative, it means that it starts from the following, for example
-1 means starting from the penultimate element, and so on.
The intercepted interval range is [start, end), the front and the opening interval is closed, and the start must be less than end
If no element is found, an empty array is returned, that is, the length of the array is 0
vararr=[3,5,11,6,90,0];vara=arr.slice(2,4);alert(a.join());splice(index,howmany,element1,.....,elementX):
Used to delete zero or more elements starting at index and declared with one or
Multiple values replace those deleted elements and return a new array of the elements that have just been deleted.
Note: This method is a direct operation of the original array object and will not create a copy of the object.
The first parameter: indicates that it starts deletion from the index position, and the index is calculated from zero
The second parameter: It means that several elements are deleted continuously from the index position. The first two parameters are required, and the subsequent parameters are optional.
The following parameters are used to add elements, and the added elements are added from the index. If the number of elements added later is greater than
If the number of elements actually deleted is more, the subsequent elements will be moved backwards by a few bits. For example, you actually deleted 4 elements.
In fact, if you add 6 elements later, you will eventually add 6 elements from the index. Since only 4 elements are deleted in the front,
The position is not enough, so the subsequent element will automatically move 2 bits backwards.
vararr=[3,5,11,6,90,0,15,57,70,20];vara=arr.splice(0,4,1,2,3,4,5,6);alert(a);for(varxinarr){alert(arr[x]);}unshift(element1,......,element):
Add one or more elements to the beginning of the array and return the added array length. At least one parameter must be passed.
Note that this method is to directly manipulate the original array, and the final added element index=0, and so on.
vararr=[3,5,11,6,90,0,15,57,70,20];arr.unshift(22,23,24);alert(arr.toString());alert(arr.length);
Functions that extend Array:
Array.prototype.indexOf=function(o){for(vari=0,len=this.length;i<len;i++){if(this[i]==o){returni;}}return-1;}Array.prototype.remove=function(o){variindex=this.indexOf(o);if(index!=-1){this.splice(index,1);}returnth;}vararr=[3,5,11,6,90,0,15,57,70,20];arr.remove(90);alert(arr.toString());alert(arr.length);Common methods of Number class in js:
toFixed(): Round the number to the number of decimal places, the parameter value range is [0,20], indicating the number of decimal places retained after rounding.
If no incoming parameters are passed, the default parameter value is equal to 0
varnum=12.5563;alert(num.toFixed());alert(num.toFixed(2));
toprecision(): used to accurately mark the number to the specified length. The method receives a parameter with a parameter with the range of parameters [0,21]
Parameters represent the number of digits. If the total number of digits is greater than the parameter value and the number is a decimal, then it will be performed.
Rounding, if the total number of digits is less than the parameter value and the number is a decimal, then the extra decimal places will automatically fill in zero.
If the total number of digits is smaller than the parameter value and the number is an integer, it will be represented by scientific notation instead.
varnum1=100009;varnum2=100;varnum3=111111111.00009;varnum4=1.00609;alert(num1.toPrecision(5));alert(num2.toPrecision(5));alert(num3.toPrecision(15));alert(num4.toPrecision(3));
isNaN(num): This method is very useful
Common methods of String class in js:
charAt(index): used to return the character at the specified position, index is calculated from 0
charCodeAt(index): ASCII code used to return the specified character
concat(element1, element2......elementx): used to splice two or more strings
indexOf(): is used to return the index of the first occurrence of the specified character in the string, search from the first character, and return immediately after finding it.
lastIndexOf(): is used to return the index of the first occurrence of the specified character in the string, but it starts from the last character.
match(): is used to retrieve substrings that match the specified regularity. If the global search mode is enabled and there are multiple substrings that meet the criteria, then
Returns an array.
varstr="helloworld!howareyou?whatareyoudoing?";vararr=str.match(/you/g);alert(arr);varstr="1plus2equal3"alert(str.match(//d/s/g));
replace(): used for string replacement operation, accepting two parameters.
The first parameter: represents the string to be replaced, or the regular expression to be replaced
The second parameter: replace text, or it can be the return value of a function
Note that this method does not change the original string object, but returns the new string object.
varstr="IlikeJava,Javaissoeasytolearning!Let'stogetherforJava";vartest=str.replace(/Java/g,"Javascript");alert(str);alert(test);varname="Doe,John";alert(name.replace(/(/w+)/s*,/s*(/w+)/,"$2|$1"));varname="Ilikejava,javaissoeasy!";vartest=name.replace(/java/g,function(m,i){alert(m);alert(i);return"javascript";});alert(test);When using the function return value as the replacement text, there are two parameters in the function:
m represents the first parameter, the substring that is the regular match, and the second parameter is the index position of the substring in the original string
search(): used to return the index of the first occurrence of a specified substring or substring that conforms to the specified regular expression in the original string.
If not found, return -1
var str = "I like javascript.";
alert(str.search("javascript"));
slice(start, end): used to intercept the string within the specified interval of start and end and return it.
This method does not manipulate the original string object data, but creates a copy of the string to save the intercepted string data
If end is not specified, it means starting directly to the end of the array.
If start or end is negative, it means that it starts from the following, for example
-1 means starting from the penultimate element, and so on.
The intercepted interval range is [start, end), the front and the opening interval is closed, and the start must be less than end
If no element is found, an empty string is returned
varstr="helloworld!";vartest=str.slice(-2,-1);alert(test);alert(str);
split(): splits the original string with the specified split character or matching character of the regular expression, and returns the result as an array.
This method can also receive a second parameter, which can limit the maximum number of array elements that will be returned in the final return.
var str="How are you doing today?"
alert(str.split(//s/));
substr(): used for string intercepting, the method receives two parameters,
The first parameter start means to intercept from the start index position, and the index starts from 0. If the value of this parameter is a negative number,
Then the calculation will start from the end of the string, such as -1 means the last character, -2 means the second to last character, and so on.
The second parameter length represents the intercepted string length. This parameter is optional. If this parameter is not specified,
By default, it will be intercepted until the end of the string.
Note: This method is no longer recommended
var str = "I like javascript!";
alert(str.substr(7,10));
substring(): used to intercept strings within the index interval of start and end. The interval range is [start, end], closed front and opened afterwards.
Note: the parameters start and end must be non-negative integers.
If start is negative, start will be assigned to 0 by default.
If end is a negative number, end will be assigned to 0 by default, and the intercept interval will be changed to [0, start).
If start is greater than end, the positions of the two parameter values will be exchanged first, that is, the interval is changed to [end, start)
varstr1="Ilikejavascript!":alert(str1.substring(7,18));varstr2="Ilikejavascript!";alert(str2.substring(3,-3));
toLowerCase(): Convert string to lowercase
toUpperCase(): Convert string to uppercase
Common methods of Date objects in js:
Date(): This method is the constructor of the Date class. This method receives a date format string.
If the constructor is not passed, the default is to take the current system time.
The constructor can receive a millisecond number from 1970-01-01 to construct a Date object.
You can also receive date strings in a specified format to build a Date object.
//var date = new Date("06 05,1987"); // Firefox OK IE is not OK
//var date = new Date("6 5,1987"); // Firefox OK IE is not OK
//var date = new Date("06 05,1987 23:12:33"); // Firefox OK IE is not OK
//var date = new Date("6 5,1987 23:12:33"); // Firefox OK IE is not OK
//var date = new Date("1987,06 05"); // Firefox OK IE is not OK
//var date = new Date("1987,6 5"); // Firefox OK IE is not OK
//var date = new Date("1987,06,05"); // Firefox OK IE is not OK
//var date = new Date("1987,6,5"); // Firefox OK IE is not OK
//var date = new Date("1987,06 05,23:12:33"); // Firefox OK IE is not OK
//var date = new Date("1987,6 5,23:12:33"); // Firefox OK IE is not OK
//var date = new Date("1987,06,05,23:12:33"); // Firefox OK IE is not OK
//var date = new Date("1987,6,5,23:12:33"); // Firefox OK IE is not OK
//var date = new Date("1987/6/5,23:12:33"); // Firefox and IE are OK
//var date = new Date("1987/06/05,23:12:33"); // Firefox and IE are OK
//var date = new Date("06/05/1987,23:12:33"); // Firefox and IE are OK
//var date = new Date("6/5/1987,23:12:33"); // Firefox and IE are OK
//var date = new Date("1987/6/5"); // Firefox and IE are OK
//var date = new Date("1987/06/05"); // Firefox and IE are OK
//var date = new Date("06/05/1987"); // Firefox and IE are OK
var date = new Date("6/5/1987"); // Firefox and IE are OK
//var date = new Date("06-05-1987"); //IE OK, Firefox is not OK
//var date = new Date("6-5-1987"); //IE OK, Firefox is not OK
//var date = new Date("1987-06-05"); // Firefox OK, IE is not OK
alert(date);
The examples above are sufficient to illustrate that if the Date() constructor needs to receive a date format string,
Then the string format should be given as follows:
yyyy/m/d
yyyy/MM/d
yyyy/m/d HH:mm:ss
yyyy/MM/d HH:mm:ss
m/d/yyyy
MM/dd/yyyy
m/d/yyyy HH:mm:ss
MM/dd/yyyy HH:mm:ss
getDate(): Returns a day of a month, the return value range: 1-31
getDay(): Returns the day of the week, which is the day of the week, the return value range: 0-6, 0 represents Sunday, 6 represents Saturday
getMonth(): Returns the month number in the date, the return value range: 0-11, 0 represents January, 11 represents December, this is a bit abnormal
getFullYear(): Returns the year number in the date, represented by 4 digits instead of 2 digits abbreviation
getHours(): Return the number of hours, return value range: 0-23
getMinutes(): Returns the number of minutes: Returns the range of value: 0 -59
getSeconds(): Returns the number of seconds, return value range: 0-59
getMilliseconds(): Returns the number of milliseconds, return value range: 0-999. I cannot understand this method name, why is the first letter of Seconds not capitalized?
getTime(): Returns the number of milliseconds of the specified date between 00:00:00 on January 1, 1970.
parse(): Used to convert a date string that complies with the specified date into a date and return the number of milliseconds from that date to 1970-01-01
Note: This method is a static method, and it cannot be called with the Date object, but should be called with the Date class.
//var date = Date.parse("1987-06-05"); // Firefox OK, IE not OK
//var date = Date.parse("06-05-1987"); //IE OK, Firefox is not OK
//var date = Date.parse("06/05/1987"); //IE and Firefox are OK
var date = Date.parse("1987/06/05"); //IE and Firefox are OK
//var date = Date.parse("6/5/1987"); //IE and Firefox are OK
//var date = Date.parse("1987/6/5"); //IE and Firefox are OK
//var date = Date.parse("1987/06/05 23:12:22"); //IE and Firefox are OK
//var date = Date.parse("6/5/1987 23:12:22"); //IE and Firefox are OK
//var date = Date.parse("1987/6/5 23:12:22"); //IE and Firefox are OK
alert(date);
Through the above examples, it is not difficult to see that the date string format received by the parse method is relatively compatible with:
yyyy/MM/dd
yyyy/m/d
MM/dd/yyyy
M/d/yyyy
yyyy/MM/dd HH:mm:ss
yyyy/m/d HH:mm:ss
MM/dd/yyyy HH:mm:ss
M/d/yyyy HH:mm:ss
setDate(): Set a certain day of a month, the value range: 1-31
setDay(): Set the day of the week, which is the day of the week, the value range is: 0-6, 0 means Sunday, 6 means Saturday
setMonth(): Set the month number in the date, the value range is: 0-11, 0 means January, 11 means December, this is a bit abnormal
setFullYear(): Set the year number in the date, represented by 4 digits instead of 2 digits abbreviation
setHours(): Set the number of hours, value range: 0-23
setMinutes(): Set the number of minutes: value range: 0 -59
setSeconds(): Set the number of seconds, value range: 0-59
setMilliseconds(): Set the number of milliseconds, the value range is: 0-999. I cannot understand this method, why is the first letter of Seconds not capitalized?
setTime(): Sets the number of milliseconds between 00:00:00 on January 1, 1970.
toString(): Convert the Date object into a string form, the default is the Greenwich standard time format, i.e. the GMT format
toTimeString(): Convert the time part of the Date object into a string form, GMT format
toDateString(): Convert the date part of the Date object into a string form, GMT format
toLocaleString: According to the date rules of the local language, the Chinese version is yyyy year MM month dd date hh:mm:ss
Date.UTC(year, month, day, hours, minutes, seconds, ms):
This method is used to return the number of milliseconds of 1970-01-01 according to the world time. The first 3 parameters are required, and the remaining parameters are optional.
It represents year, month, day, hour, minute, second, millisecond, respectively.
The number of milliseconds returned by this method can be passed to the Date() constructor.
The toString method of the Date object is converted to GMT format by default. For us, it is not applicable. We often want to display it in yyyy-MM-dd hh:mm:ss format.
The Date native object does not provide this function, so it has to expand it itself.
Date.prototype.format=function(format){varo={"M+":this.getMonth()+1,//month month "d+":this.getDate(),//day day "h+":this.getHours(),//hour time "m+":this.getMinutes(),//minute minute "s+":this.getSeconds(),//second second "q+":Math.floor((this.getMonth()+3)/3),//quarter quarter "S":this.getMilliseconds()//millis econd milliseconds}if(/(y+)/.test(format)){format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));}for(varkino){if(newRegExp("("+k+")").test(format)){format=format.replace(RegExp.$1,RegExp.$1.length==1?o[k]:("00"+o[k]).substr((""+o[k]).length));}}returnformat;}Example of usage:
vardate=newDate();alert(date.format("yyyy-MM-ddhh:mm:ss"));The native Date class of js also does not provide an add method, that is, add or subtract the specified number of days based on the original date. Now it is expanded as follows:
Date.prototype.dateAdd=function(interval,number){vard=this;vark={'y':'FullYear','q':'Month','m':'Month','w':'Date','d':'Date','h':'Hours','n':'Minutes','s':'Seconds','ms':'MilliSeconds'};varn={'q':3,'w':7};eval('d.set'+k[interval]+'(d.get'+k[interval]+'()+'+((n[interval]||1)*number)+')');returnd;}interval parameters:
y year
q Quarterly
m month
d Day
w
h hours
n minutes
s seconds
ms milliseconds
number parameter: time interval, must be a number, a positive number indicates the future date of the specified interval, and a negative number indicates the past date
// Used to calculate the time interval between two dates,
//Use this method to compare the sizes of two dates. If the return value is greater than 0, it means that objDate2 is relatively large.
//If it is less than 0, it means that objDate2 is smaller
Date.prototype.dateDiff = function(interval,objDate2){ var d=this, i={}, t=d.getTime(), t2=objDate2.getTime(); i['y']=objDate2.getFullYear()-d.getFullYear(); i['q']=i['y']*4+Math.floor(objDate2.getMonth()/4)-Math.floor(d.getMonth()/4); i['m']=i['y']*12+objDate2.getMonth()-d.getMonth(); i['ms']=objDate2.getTime()-d.getTime(); i['w']=Math.floor((t2+345600000)/(604800000))-Math.floor((t+345600000)/(604800000)); i['d']=Math.floor(t2/86400000)-Math.floor(t/86400000); i['h']=Math.floor(t2/3600000)-Math.floor(t/3600000); i['n']=Math.floor(t2/60000)-Math.floor(t/60000); i['n']=Math.floor(t2/60000)-Math.floor(t/60000); i['s']=Math.floor(t2/1000)-Math.floor(t/1000); return i[interval];}interval parameters: refer to the interval parameters description of dateAdd method above.
objDate2: Another date object
Math class in js:
This class is a static class and cannot create instances through constructors, so the provided methods are all static methods and are called directly through the class name.
abs(): Get the absolute value of a number. If the provided parameter is a string, it will first try to convert it to a number. If it cannot
When converted to a number, it will directly return NaN, and if possible, it will return its absolute value.
ceil(): performs up-rounding calculation on the passed parameters. If the passed in is not a number, it will try to convert it numerically.
If it cannot be converted, it will directly return NaN.
floor(): performs a down-round calculation on the passed parameter. If the passed parameter is not a number, it will try to convert it numerically.
If it cannot be converted, it will directly return NaN.
max(x1,x2,x3......xn): Returns the maximum value in the specified parameter. If one of the specified parameters cannot be converted into a number, then it will be directly
Returns NaN, if no incoming parameters are passed, returns negative infinity
min(x1,x2,x3......xn): Returns the minimum value in the specified parameter. If one of the specified parameters cannot be converted into a number, then it will be directly
Returns NaN, if no incoming parameters are passed, returns positive infinity
pow(x,y): Returns the y power of x. If the calculation result is a negative number, it returns NaN. If the calculation result is too large, floating point overflow.
Return to the infinity
random(): Returns a random number between 0 and 1.
round(x): The integer closest to x. If x is a positive number, then 0.5 will be converted to 1. If it is -0.5, then it will be abandoned.
-0.50001 will be converted to -1
sqrt(x): Returns the square root of a number. If x is less than 0, it returns NaN,
If the passed in is not a number, it will try to convert it digitally.
If it cannot be converted, it will directly return NaN.
RegExp regular object for js:
There are two ways to create regular objects:
1. /pattern/attributes
pattern is the regular expression part,
attributes: optional parameters, including attributes "g", "i" and "m" and other values
g: means global match (find all matches instead of stopping after finding the first match)
i: means that upper and lower case is ignored
m: means multiple row matching
2, new RegExp(pattern, attributes), the second parameter can be selected
The above summary of common methods for JavaScript native objects (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.