1. JavaScript is an object-oriented programming language
Encapsulation: the ability to store relevant information (whether data or methods) in an object
Aggregation: The ability to store one object within another object
Inheritance: The ability to obtain the properties and methods of a class from another class (or classes).
Polymorphism: The ability to write functions or methods that can run in multiple forms
2. Array object
Use separate variable names to store a range of values.
2.1 Create array object
var aValues = new Array(); var aValues = new Array(25); var aColors = new Array("red","green","blue"); alert(aColors[1]);//输出green aColors[0] = "white"; //修改第一个元素的值 var aColors = new Array(); aColors[0] = "red"; aColors[1] = "green"; aColors[2] = "blue";
2.2 Create Array using literals
var aColors = ["red","green","blue"]; aColors[3]="black";
2.3 The length of the array
var aColors = new Array("red","green","blue“); alert(aColors.length);//输出3 var aColors = new Array("red","green","blue“); alert(aColors.length);//输出3 aColors[20]="black"; alert(aColors.length);//输出21 alert(aColors[3]);//输出undefined
2.4 Array Traversal
var subjects=["html/css/js","Web form","ajax","N-Tiers","oracle"]; for(var i=0;i<subjects.length;i++){ alert(subjects[i]); } var subjects=["html/css/js","Web form","ajax","N-Tiers","oracle"]; for(var index in subjects){ alert(subjects[index]); }
2.5 Array attributes and methods
length sets or returns the number of elements in the array.
join() concatenates all elements of the array into a string using the specified delimiter.
reverse() reverses the order of elements in an array.
sort() sorts the elements of the array
arrayObject.sort(sortby)
Parameters are optional. Specify sorting rules. Must be a function.
The return value is a reference to the array. Please note that the array is sorted on the original array, no copy is made.
Customized sorting rules
If you want to sort by other criteria, you need to provide a comparison function, which compares two values and returns a number that describes the relative order of the two values.
The comparison function should have two parameters a and b, and its return value is as follows: if a is less than b, a should appear before b in the sorted array, then return a value less than 0. If a equals b, returns 0. If a is greater than b, returns a value greater than 0.
concat(Array array)
Concatenate two or more arrays and return the result.
Array slice(Number start,Number end)
Return selected elements from an existing array
var messages4 = messages3.slice(0, 5);
splice() removes elements and adds new elements to the array.
messages4.splice(0, 2, "dddd"); //0 indicates the position of the element to start deleting, 2 indicates the length of deletion, the added element is dddd, add
at the deletion position
push() adds one or more elements to the end of the array and returns the new length.
pop() removes and returns the last element of the array
unshift() adds one or more elements to the beginning of the array and returns the new length.
shift() deletes and returns the first element of the array
2.6 Application of Arrays
var messages = new Array("","",""); messages[0] = "beijing"; messages[1] = "shanghai"; messages[2] = "zhejiang"; messages[3] = "hunan"; for ( var index in messages) { document.writeln("遍历数组"); document.write(messages[index]); document.write("<br>"); } var s = messages.join(','); document.writeln("join数组"); document.write(s); document.write("<br>"); messages.reverse(); for ( var index in messages) { document.writeln("倒序后"); document.write(messages[index]); document.write("<br>"); } messages.sort(); for ( var index in messages) { document.writeln("排序后"); document.write(messages[index]); document.write("<hr>"); } var messages2 = ["a","c","b"]; var messages3 = messages.concat(messages2); for ( var index in messages3) { document.writeln("合并后"); document.write(messages3[index]); document.write("<hr>"); } var messages4 = messages3.slice(0, 5); for ( var index in messages4) { document.writeln("选择slice(0,5)后"); document.write(messages4[index]); document.write("<hr>"); }
The output result of the above code is:
Traverse the array beijing
Traverse the array shanghai
Traverse the array zhejiang
Traverse the array hunan
join array beijing,shanghai,zhejiang,hunan
After reverse order hunan
After reverse order zhejiang
After reverse order shanghai
Beijing after reverse order
After sorting beijing
After sorting hunan
After sorting shanghai
After sorting zhejiang
Beijing after merger
After merger hunan
After merger shanghai
After merger zhejiang
After merge a
After merge c
After merge b
After selecting slice(0,5), beijing
After selecting slice(0,5) hunan
After selecting slice(0,5) shanghai
After selecting slice(0,5) zhejiang
After selecting slice(0,5) a
3. Date object
Date objects are used to obtain and process dates and times.
3.1 Define date
var myDate=new Date() ; var myDate=new Date(2009 , 7 , 20); //2009-08-20 var myDate=new Date(2009 , 7 , 20 , 16, 40 ,58, 999); //依次表示年月日时分秒毫秒
3.2日期对象的方法
3.2.1toXXX方法组
toString() 把 Date 对象转换为字符串。
toTimeString()
把 Date 对象的时间部分转换为字符串。
toDateString()
把 Date 对象的日期部分转换为字符串。
toGMTString() 根据格林尼治时间,把 Date 对象转换为字符串。
toUTCString() 根据世界时,把 Date 对象转换为字符串。
toLocaleString()
把 Date 对象转换为本地时间格式字符串。
toLocaleTimeString()
把 Date 对象的时间部分转换为本地时间格式字符串
toLocaleDateString()
把 Date 对象的日期部分转换为本地时间格式字符串
3.2.2getXXX方法组
getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)
getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)
getMonth() 从 Date 对象返回月份 (0 ~ 11)
getFullYear() 从 Date 对象以四位数字返回年份
getYear() 从 Date 对象以两位或四位数字返回年份。
getHours() 返回 Date 对象的小时 (0 ~ 23)
getMinutes() 返回 Date 对象的分钟 (0 ~ 59)
getSeconds() 返回 Date 对象的秒数 (0 ~ 59))
getMilliseconds()
返回 Date 对象的毫秒(0 ~ 999)
getTime() 返回 1970 年 1 月 1 日至今的毫秒数
3.2.3setXXX方法组
setDate() 设置 Date 对象中月的某一天 (1 ~ 31))
setMonth() 设置 Date 对象中月份 (0 ~ 11))
setFullYear() 设置 Date 对象中的年份(四位数字)
setYear() 设置 Date 对象中的年份(两位或四位数字)。
setHours() 设置 Date 对象中的小时 (0 ~ 23)
setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)
setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)
setMilliseconds()
设置 Date 对象中的毫秒 (0 ~ 999)
setTime() 通过向时间原点添加或减去指定毫秒数来计算日期和时间
四、String对象
String对象表示Unicode字符的序列。
String 对象的 length 属性声明了该字符串中的字符数。
String 类定义了大量操作字符串的方法。
4.1属性
length 字符串的长度
4.2与字符串格式控制相关的方法
big()、small() 用大号字体、小字号体显示字符串。
bold() 使用粗体显示字符串。
italics() 使用斜体显示字符串。
strike() 使用删除线来显示字符串。
fontcolor() 使用指定的颜色来显示字符串。
fontsize() 使用指定的尺寸来显示字符串。
sub()、sup() 把字符串显示为下标、上标。
toLowerCase()、toUpperCase()
把字符串转换为小写、大写。
fixed() 以打字机文本显示字符串。
link() 将字符串显示为链接。
anchor() 创建 HTML 锚。
4.3与字符串内容操作相关的方法
charAt() 返回在指定位置的字符。
charCodeAt() 返回在指定的位置的字符的 Unicode 编码。
concat() 连接字符串。
fromCharCode()
从字符编码创建一个字符串。
indexOf()、lastIndexOf()
从前向后检索字符串。从后向前搜索字符串。
localeCompare()
用本地特定的顺序来比较两个字符串。
match() 找到一个或多个正在表达式的匹配。
replace()、search()
替换、检索与正则表达式匹配的子串。
slice() 提取字符串的片断。
split() 把字符串分割为字符串数组。
substr() 从起始索引号提取字符串中指定数目的字符。
substring() 提取字符串中两个指定的索引号之间的字符。
4.4应用
var strings = new String("AbcDEfG"); document.write("strings中的字符串值:"); document.write(strings); document.write('<hr>'); document.write("strings.big()大号字体显示:"); document.write(strings.big()); document.write('<hr>'); document.write("strings.small()小号字体显示:"); document.write(strings.small()); document.write('<hr>'); document.write("strings.bold()粗体显示:"); document.write(strings.bold()); document.write('<hr>'); document.write("strings.italics()斜体显示:"); document.write(strings.italics()); document.write('<hr>'); document.write("strings.strike()删除线显示:"); document.write(strings.strike()); document.write('<hr>'); document.write("strings.fontcolor('green')指定颜色显示:"); document.write(strings.fontcolor('green')); document.write('<hr>'); document.write("strings.fontsize(10)指定大小显示:"); document.write(strings.fontsize(10)); document.write('<hr>'); document.write("strings.sub()下标显示:"); document.write("123"+ strings.sub()); document.write('<hr>'); document.write("strings.sup()上标显示:"); document.write("123"+strings.sup()); document.write('<hr>'); document.write("strings.toLocaleLowerCase()小写显示:"); document.write(strings.toLocaleLowerCase()); document.write('<hr>'); document.write("strings.toLocaleUpperCase()大写显示:"); document.write(strings.toLocaleUpperCase()); document.write('<hr>'); document.write("strings.fixed()打字机文本显示:"); document.write(strings.fixed()); document.write('<hr>'); document.write("strings.link()链接显示:"); document.write(strings.link()); document.write('<hr>'); document.write("strings.anchor()创建HTML锚显示:"); document.write(strings.anchor()); document.write('<hr>'); document.write("strings.charAt(2)返回指定位置的字符:"); document.write(strings.charAt(2)); document.write('<hr>'); document.write("strings.charCodeAt(2)返回指定位置的字符的unicode编码:"); document.write(strings.charCodeAt(2)); document.write('<hr>'); document.write("strings.concat('higkL')连接字符串:"); document.write(strings.concat("higkL")); document.write('<hr>'); document.write("String.fromCharCode(98)静态方法从字符编码创建一个字符串:"); document.write(String.fromCharCode(98)); document.write('<hr>'); document.write("strings.indexOf('G', 1)从指定1处开始从前向后检索字符,返回一个位置值:"); document.write(strings.indexOf('G', 1)); document.write('<hr>'); document.write("strings.lastIndexOf('G', 1)从指定1处开始从后向前检索字符,找不到指定字符位置返回-1.返回一个位置值:"); document.write(strings.lastIndexOf('G', 1)); document.write('<hr>'); document.write("strings.localeCompare('ABcdEfG'):'ABcdEfG'从第0个位置开始比较'AbcDEfG',如果两个字符串完全相同返回0,被比较的字符串ASSIC码值要小,则返回-1,大则返回1。用本地特定的顺序来比较两个字符串:"); document.write(strings.localeCompare("ABcdEfG")); document.write('<hr>'); document.write("strings.match('[a-z]{2}'):'AbcDEfG',返回值为bc,表示匹配到了。匹配不到返回null。找到一个或多个正则表达式的匹配:"); document.write(strings.match('[a-z]{2}')); document.write('<hr>'); document.write("strings.search('[a-z]'):'AbcDEfG',返回值为下标1,表示检索到了。匹配不到返回-1。找到一个或多个正则表达式的匹配:"); document.write(strings.search('[a-z]')); document.write('<hr>'); document.write('strings.slice(2, 3)参数1表示开始位置,参数2表示结束位置,不包括该位置。提取字符串:'); document.write(strings.slice(2, 3)); document.write('<hr>'); document.write('strings.substring(2, 3)参数1表示开始位置,参数2表示结束位置,不包括该位置。提取字符串:'); document.write(strings.substring(2, 3)); document.write('<hr>'); 以上代码输出为 strings中的字符串值:AbcDEfG strings.big()大号字体显示:AbcDEfG strings.small()小号字体显示:AbcDEfG strings.bold()粗体显示:AbcDEfG strings.italics()斜体显示:AbcDEfG strings.strike()删除线显示:AbcDEfG strings.fontcolor('green')指定颜色显示:AbcDEfG strings.fontsize(10)指定大小显示:AbcDEfG strings.sub()下标显示:123AbcDEfG strings.sup()上标显示:123AbcDEfG strings.toLocaleLowerCase()小写显示:abcdefg strings.toLocaleUpperCase()大写显示:ABCDEFG strings.fixed()打字机文本显示:AbcDEfG strings.link()链接显示:AbcDEfG strings.anchor()创建HTML锚显示:AbcDEfG strings.charAt(2)返回指定位置的字符:c strings.charCodeAt(2)返回指定位置的字符的unicode编码:99 strings.concat('higkL')连接字符串:AbcDEfGhigkL String.fromCharCode(98)静态方法从字符编码创建一个字符串:b strings.indexOf('G', 1)从指定1处开始从前向后检索字符,返回一个位置值:6 strings.lastIndexOf('G', 1)从指定1处开始从后向前检索字符,找不到指定字符位置返回-1.返回一个位置值:-1 strings.localeCompare('ABcdEfG'):'ABcdEfG'从第0个位置开始比较'AbcDEfG',如果两个字符串完全相同返回0,被比较的字符串ASSIC码值要小,则返回-1,大则返回1。用本地特定的顺序来比较两个字符串:-1 strings.match('[a-z]{2}'):'AbcDEfG',返回值为bc,表示匹配到了。匹配不到返回null。找到一个或多个正则表达式的匹配:bc strings.search('[a-z]'):'AbcDEfG',返回值为下标1,表示检索到了。匹配不到返回-1。找到一个或多个正则表达式的匹配:1 strings.slice(2, 3)参数1表示开始位置,参数2表示结束位置,不包括该位置。提取字符串:c strings.substring(2, 3)参数1表示开始位置,参数2表示结束位置,不包括该位置。提取字符串:c
5. Math object
The Math object is a mathematical tool that contains many static read-only properties representing mathematical constants and static methods for performing mathematical operations.
E constant e, the base of natural logarithms (approximately equal to 2.718)
LN2 returns the natural logarithm of 2 (approximately equal to 0.693)
LN10 returns the natural logarithm of 10 (approximately equal to 2.302)
LOG2E returns the base 2 logarithm of e (approximately 1.414)
LOG10E returns the base 10 logarithm of e (approximately 0.434)
PI returns pi (approximately equal to 3.14159)
SQRT1_2 returns the square root of 2 divided by 1 (approximately 0.707)
SQRT2 returns the square root of 2 (approximately 1.414)
abs(x) returns the absolute value of the number
cos(x), acos(x)
Returns the sine and arc cosine values of the number
sin(x), asin(x)
Returns the sine and arcsine values of the number
tan(x), atan(x)
Returns the tangent of the angle and returns the arctangent of the number of radians between -PI/2 and PI/2
atan2(y,x) returns the angle from the x-axis to the point (x,y)
ceil(x)、floor(x)
Round up a number. Round down a number.
exp(x), log(x) returns the exponent of e. Returns the natural logarithm of the number (base e)
max(x,y), min(x,y)
Return the highest value between x and y
pow(x,y) returns x raised to the y power
random() returns a random number between 0 ~ 1
round(x) rounds a number to the nearest integer
sqrt(x) returns the square root of the number
6. Global Object
Global objects are predefined objects that serve as placeholders for JavaScript’s global functions and global properties.
By using the global object, all other predefined objects, functions and properties can be accessed.
The global object is not a property of any object, so it has no name. It has neither a constructor nor the ability to instantiate a new global object, and all its members are static.
6.1 Global Properties
Infinity represents a positive infinity value.
NaN indicates whether a value is a numeric value.
undefined indicates an undefined value.
6.2 Global Functions
decodeURI() decodes an encoded URI.
decodeURIComponent()
Decode an encoded URI component.
encodeURI() Encodes a string into a URI.
encodeURIComponent()
Encode a string into a URI component.
eval() evaluates a string and executes it as script code.
isFinite() checks whether a value is a finite number.
isNaN() checks whether a value is a number.
The isNaN() function is usually used to detect the results of parseFloat() and parseInt() to determine whether they represent legal numbers. Of course, you can also use the isNaN() function to detect arithmetic errors, such as using 0 as a divisor.
parseFloat() parses a string and returns a floating point number.
parseInt() parses a string and returns an integer.
7. Summary
JavaScript is an object-oriented language with the ability to customize objects. At the same time, it can be programmed based on existing objects, including local objects of ECMAScript, and the browser running environment as the host provides a set of browser objects and objects provided by web pages.
Local objects are a set of reference types provided by ECMAScript. They are some objects implemented by ECMAScript and have nothing to do with a specific script host.
The array object is actually a collection with dynamic length, which contains length attributes and methods such as sorting, reversal, and splicing.
A date object represents a specific moment in time. You can get the system time of the browser through its constructor, or specify a specific date and time. It contains a set of get methods that can be used to obtain certain part of the information in the date object, and you can also use its set of get methods to change the date object. Some methods, or obtain the string form of the date object through its set of to methods.
A string object represents a sequence of 0 or more Unicode characters. It contains the length attribute, and a set of methods for format conversion and methods for operating characters.
Global objects contain a series of global properties and global functions, which can be used directly without referencing any objects.
Mathematical objects contain a set of static read-only properties used to represent some mathematical constants, and a set of static methods used to implement common mathematical operations.
This editor will introduce you to this summary of commonly used local objects in JavaScript. I hope it will be helpful to you!