引用類型
Object類型
- 創(chuàng)建
創(chuàng)建Object實(shí)例有兩種方式- 使用new操作符后跟Object構(gòu)造函數(shù)
- 使用對(duì)象字面量表示法(不會(huì)調(diào)用構(gòu)造函數(shù))
var person = new Object(); //同 var person = {}; var person = new Object(); person.name = "Tom"; person.age = 18; var person = { name:"Tom", // 同 "name":"Tom", age:18 };
注
:對(duì)象字面量也是向函數(shù)傳遞大量可選參數(shù)的首先方式祠肥,如function displayInfo(args) { var output = ""; if (typeof args.name == "string"){ output += "Name: " + args.name + "\n"; } if (typeof args.age == "number"){ output += "Age: " + args.age + "\n"; } console.log(output); } displayInfo({ name : "Tom", age : 18 }); displayInfo({ name : "ming" });
- 訪問
訪問方式有兩種- 點(diǎn)表示法
- 方括號(hào)表示法
console.log(person.name); console.log(person["name"]);
Array類型
ECMAScript數(shù)組同其他語言一樣都是數(shù)據(jù)的有序列表,ECMAScript數(shù)組的每一項(xiàng)可以保存任何類型的數(shù)據(jù),而且ECMAScript數(shù)組的大小是可以動(dòng)態(tài)調(diào)整的侨艾,即可以隨著數(shù)據(jù)的添加自動(dòng)增長(zhǎng)以容納新增數(shù)據(jù)
- 創(chuàng)建
創(chuàng)建數(shù)組有兩種方式- 使用Array構(gòu)造函數(shù)
- 使用數(shù)組字面量表示法(不會(huì)調(diào)用構(gòu)造函數(shù))
var colors = new Array("red","blue","green"); var colors = ["red","blue","green"];
- 檢測(cè)數(shù)組
為了避免多框架所帶來的多個(gè)不同的執(zhí)行環(huán)境所帶來的差異越庇,使用如下方法來檢測(cè)數(shù)組if (Array.isArray(value)){ //對(duì)數(shù)組執(zhí)行某些操作 }
- 打印字符串
var colors = ["red","blue","green"]; console.log(colors.toString()); console.log(colors); console.log(colors.valueOf());
- 棧方法
ECMAScript數(shù)組也提供了棧的方法-
push()
進(jìn)棧 -
pop()
出棧
var colors = ["red","blue"]; colors.push("green"); var item = colors.pop(); console.log(item);//green console.log(colors.length);//2
-
- 隊(duì)列方法
ECMAScript數(shù)組也提供了隊(duì)列的方法-
push()
進(jìn)隊(duì) -
shift()
出隊(duì)
var colors = ["red","blue"]; colors.push("green"); var item = colors.shift(); console.log(item);//red console.log(colors.length);//2
-
- 反向隊(duì)列
即在數(shù)組前端添加項(xiàng)弹谁,從數(shù)組末端移除項(xiàng)-
unshift()
在數(shù)組前端添加任意個(gè)項(xiàng)并返回長(zhǎng)度 pop()
var colors = ["red","blue"]; colors.unshift("green"); console.log(colors);//[ 'green', 'red', 'blue' ] var item = colors.pop(); console.log(item);//blue console.log(colors.length);//2
-
- 排序
使用sort()
和reverse()
進(jìn)行排序漾稀,reverse()
會(huì)反轉(zhuǎn)數(shù)組的順序派近,sort()
會(huì)重小到大進(jìn)行排序萄金,同時(shí)還可以自定義排序方式var students = [{name:"stu1",age:12},{name:"stu2",age:13},{name:"stu3",age:15},{name:"stu4",age:10}]; students.sort((value1, value2) => { if (value1.age < value2.age){ return -1; } else if (value1.age > value2.age){ return 1; }else { return 0; } }); students.sort(function (value1,value2) { if (value1.age < value2.age){ return -1; } else if (value1.age > value2.age){ return 1; }else { return 0; } }); console.log(students); //{ name: 'stu4', age: 10 },{ name: 'stu1', age: 12 },{ name: 'stu2', age: 13 },{ name: 'stu3', age: 15 }
- 操作方法
-
concat()
可以基于當(dāng)前數(shù)組中的所有項(xiàng)創(chuàng)建一個(gè)新數(shù)組蟀悦。
具體來說媚朦,這個(gè)方法會(huì)先創(chuàng)建當(dāng)前數(shù)組一個(gè)副本,然后將接收到的參數(shù)添加到這個(gè)副本的末尾日戈,最后返回新構(gòu)建的數(shù)組var colors = ["red","green","blue"]; var colors2 = colors.concat("yellowd",["black","brown"]); console.log(colors); //[ 'red', 'green', 'blue' ] console.log(colors2); //[ 'red', 'green', 'blue', 'yellowd', 'black', 'brown' ]
-
slice()
能夠基于當(dāng)前數(shù)組的一個(gè)或多個(gè)創(chuàng)建一個(gè)數(shù)組
在只有一個(gè)參數(shù)的情況下询张,slice()
方法返回從該參數(shù)指定位置開始到當(dāng)前數(shù)組末尾的所有項(xiàng)。所有有兩個(gè)參數(shù)涎拉,該方法返回起始位置和結(jié)束位置之間的項(xiàng)-不包括結(jié)束位置的項(xiàng)var colors = ["red","green","blue","yellow","purple"]; //[ 'green', 'blue', 'yellow', 'purple' ] console.log(colors.slice(1)); // [ 'green', 'blue', 'yellow' ] console.log(colors.slice(1,4));
-
splice()
刪除方法
可以刪除任意數(shù)量的項(xiàng)瑞侮,第一個(gè)參數(shù)為要?jiǎng)h除的第一項(xiàng)的位置,第二個(gè)參數(shù)為要?jiǎng)h除的項(xiàng)數(shù)
插入方法var colors = ["red","green","blue","yellow","purple"]; var removed = colors.splice(2,1); //[ 'blue' ] console.log(removed); //[ 'red', 'green', 'yellow', 'purple' ] console.log(colors);
可以向指定位置插入任意數(shù)量的項(xiàng)鼓拧,需要提供三個(gè)參數(shù):起始位置半火、0、要插入的項(xiàng)數(shù)
替換方法var colors = ["red","green","blue"]; colors.splice(1,0,"yellow");//返回null //[ 'red', 'yellow', 'green', 'blue' ] console.log(colors);
可以向指定位置插入任意數(shù)量的項(xiàng)季俩,且同時(shí)刪除任意數(shù)量的項(xiàng)钮糖。需要提供三個(gè)參數(shù):起始位置、要?jiǎng)h除的項(xiàng)數(shù)和要插入的任意數(shù)量的項(xiàng)var colors = ["red","green","blue"]; var removed = colors.splice(1,1,"yellow","black");//返回null //[ 'green' ] console.log(removed); // [ 'red', 'yellow', 'black', 'blue' ] console.log(colors);
-
- 位置方法
使用indexOf()
和lastIndexOf()
方法查找特定項(xiàng)在數(shù)組中的位置var colors = ["red","green","blue","yellow","purple"]; console.log(colors.indexOf("blue"));//2 console.log(colors.lastIndexOf("yellow"));//3
- 迭代方法
共有五種迭代方式
每個(gè)方法需要接收兩個(gè)參數(shù):要在每一項(xiàng)上運(yùn)行的函數(shù)和(可選的)運(yùn)行該函數(shù)的作用域?qū)ο蟆绊憈his值酌住。傳入這些方法中的函數(shù)會(huì)接收三個(gè)參數(shù):數(shù)組項(xiàng)的值店归、該項(xiàng)在數(shù)組中的位置和數(shù)組對(duì)象本身。-
every()
對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定函數(shù)酪我,如果該函數(shù)對(duì)每一項(xiàng)都返回true消痛,則返回true -
filter()
對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定函數(shù),返回該函數(shù)會(huì)返回true的項(xiàng)組成的數(shù)組 -
forEach()
對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定函數(shù)都哭,這個(gè)方法沒有返回值 -
map()
對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定函數(shù)秩伞,返回每次函數(shù)調(diào)用的結(jié)果組成的數(shù)組 -
some()
對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定函數(shù),如果該函數(shù)對(duì)任一項(xiàng)返回true欺矫,則返回true
var numbers = [1,2,3,4,5,4,3,2,1]; var result = numbers.every((value, index, array) => { return (value > 2); }); console.log(result);//false result = numbers.some((value, index, array) => { return (value > 2);//true }); console.log(result); result = numbers.filter((value, index, array) => { return (value > 2); }); //[ 3, 4, 5, 4, 3 ] console.log(result); result = numbers.map((value, index, array) => { return (value * 2); }); // [ 2, 4, 6, 8, 10, 8, 6, 4, 2 ] console.log(result);
-
- 歸并方法
reduce()
和reduceRight()
這兩個(gè)方法都會(huì)迭代數(shù)組的所有項(xiàng)纱新,然后構(gòu)建一個(gè)最終返回的值。reduce()從第一項(xiàng)開始向后遍歷穆趴,reduceRight()從最后一項(xiàng)開始向前遍歷
這兩個(gè)方法都接收兩個(gè)參數(shù):1個(gè)在每一項(xiàng)上調(diào)用的函數(shù)和(可選的)作為歸并基礎(chǔ)的初始值脸爱。傳入的函數(shù)接收4個(gè)參數(shù):前一個(gè)值、當(dāng)前值未妹、項(xiàng)的索引和數(shù)組對(duì)象簿废,這個(gè)函數(shù)返回的任何值都會(huì)作為第一個(gè)參數(shù)自動(dòng)傳給下一項(xiàng)。var numbers = [1,2,3,4,5]; var sum = numbers.reduceRight((previousValue, currentValue, currentIndex, array) => { return previousValue+currentValue; }); console.log(sum);//15
Function類型
函數(shù)實(shí)際上是對(duì)象络它,每個(gè)函數(shù)都是Function類型的實(shí)例族檬,而且都與其他引用類型一樣具有屬性和方法。
//函數(shù)聲明
function sum(num1,num2) {
return num1+num2;
}
console.log(sum(1,2));//3
//函數(shù)表達(dá)式
var sum = function (num1,num2) {
return num1+num2;
}
console.log(sum(1,2));//3
var sum = new Function("num1","num2","return num1+num2");
console.log(sum(1,2));//3
由于函數(shù)名僅僅是指向函數(shù)的指針酪耕,因此函數(shù)名與包含對(duì)象指針的其他變量沒有什么不同导梆。換句話說,一個(gè)函數(shù)可能會(huì)有多個(gè)名字
function sum(num1,num2) {
return num1+num2;
}
console.log(sum(1,2));
var sum1 = sum;
console.log(sum1(1,2));
sum = null;
console.log(sum1(1,2));
-
函數(shù)聲明與函數(shù)表達(dá)式
實(shí)際上,解析器在向執(zhí)行環(huán)境中加載數(shù)據(jù)時(shí)看尼,對(duì)函數(shù)聲明和函數(shù)表達(dá)式并非一視同仁递鹉。
解析器會(huì)率先讀取函數(shù)聲明,并使其在執(zhí)行任何代碼之前可用(可以訪問)
至于函數(shù)表達(dá)式藏斩,則必須等到解析器執(zhí)行到它所在的代碼行躏结,才會(huì)真正被解釋執(zhí)行console.log(sum(1,2)); function sum(num1,num2) { return num1+num2; }
以上代碼可以完全運(yùn)行
console.log(sum(1,2)); var sum = function (num1,num2) { return num1+num2; }
以上代碼不能運(yùn)行
-
函數(shù)的內(nèi)部屬性
在函數(shù)內(nèi)部,有兩個(gè)特殊的對(duì)象:arguments
和this
狰域。-
arguments
雖然arguments
的主要用途是保存函數(shù)參數(shù)媳拴,但這個(gè)對(duì)象還有一個(gè)名叫callee
的屬性,該屬性是一個(gè)指針兆览,指向擁有這個(gè)arguments
對(duì)象的函數(shù)function factorial(num) { if (num<=1){ return 1; } return num * factorial(num-1); } console.log(factorial(5));//120 //解耦 function factorial(num) { if (num<=1){ return 1; } return num * arguments.callee(num-1); } console.log(factorial(5));//120
-
this
this
引用的是函數(shù)執(zhí)行的環(huán)境對(duì)象——或者也可以說是this值(當(dāng)前網(wǎng)頁(yè)的全局作用域中調(diào)用函數(shù)時(shí)屈溉,this對(duì)象引用的就是window) -
caller
這個(gè)屬性中保存著調(diào)用當(dāng)前函數(shù)的函數(shù)的引用,如果是在全局作用域中調(diào)用當(dāng)前函數(shù)抬探,他的值為nullfunction outer() { inner(); } function inner() { console.log(arguments.callee.caller); //同 console.log(inner.caller); } outer();
-
-
函數(shù)屬性和方法
函數(shù)也有屬性和方法子巾。每個(gè)函數(shù)都包含兩個(gè)屬性:length
和prototype
-
length
length
屬性表示函數(shù)希望接收的命名參數(shù)的個(gè)數(shù)function f1() { } function f2(arg) { } function f3(arg1,arg2) { } console.log(f1.length);//0 console.log(f2.length);//1 console.log(f3.length);//2
-
prototype
是保存它們所有實(shí)例方法的真正所在
每個(gè)函數(shù)都包含兩個(gè)非繼承而來的方法
apply()
和call()
這兩個(gè)方法的用途都是在特定的作用域中調(diào)用函數(shù),實(shí)際上等于設(shè)置函數(shù)體內(nèi)this對(duì)象的值-
apply()
apply()
方法接收兩個(gè)參數(shù):一個(gè)是在其中運(yùn)行函數(shù)的作用域小压,另一個(gè)是參數(shù)數(shù)組线梗。其中第二個(gè)參數(shù)可以是Array
的實(shí)例,也可以是arguments
對(duì)象function sum(num1,num2) { return num1+num2; } function callSum1(num1,num2) { return sum.apply(this,arguments); } function callSum2(num1,num2) { return sum.apply(this,[num1,num2]); } console.log(callSum1(10,20));//30 console.log(callSum2(10,20));//30
-
call()
call()
與apply()
基本相同怠益,call()
的第一個(gè)沒有變化仪搔,變化的是其余參數(shù)都直接傳遞給函數(shù)function callSum(num1,num2) { return sum.call(this,num1,num2); } console.log(callSum(10,20));//30
注
:call()
與apply()
真正強(qiáng)大的地方是能夠擴(kuò)展函數(shù)賴以運(yùn)行的作用域window.color="red"; var o = {color:"blue"}; function sayColor() { console.log(this.color); } sayColor.call(this);//red sayColor.call(window);//red sayColor.call(o);//blue
使用
call()
與apply()
來擴(kuò)充作用域的最大好處,就是對(duì)象不需要與方法有任何耦合關(guān)系蜻牢。-
bind()
這個(gè)方法會(huì)創(chuàng)建一個(gè)函數(shù)的實(shí)例烤咧,其this值會(huì)被綁定到傳給bind()
函數(shù)的值color = "red"; var o = {color : "blue"}; function sayColor() { console.log(this.color); } var objectSayColor = sayColor.bind(o); objectSayColor();//blue
-
基本包裝類型
為了方便便于操作基本類型值,ECMAScript還提供了3個(gè)特殊的引用類型:Boolean孩饼、Number髓削、String竹挡。這些類型與其他引用類型相似镀娶,但同時(shí)也具有與各自的基本類型相應(yīng)的特殊行為。實(shí)際上揪罕,每當(dāng)讀取一個(gè)基本類型值的時(shí)候梯码,后臺(tái)就會(huì)創(chuàng)建一個(gè)對(duì)應(yīng)的基本包裝類型的對(duì)象,從而讓我們能夠調(diào)用一些方法來操作這些數(shù)據(jù)好啰。
var s1 = "some text";
var s2 = s1.substring(2);
當(dāng)?shù)诙写a訪問s1時(shí)轩娶,訪問過程處于一種讀取模式,也就是要從內(nèi)存中讀取這個(gè)字符串的值框往,而在讀取模式中訪問字符串時(shí)鳄抒,后臺(tái)都會(huì)自動(dòng)完成下列處理
- 創(chuàng)建String類型的一個(gè)實(shí)例
- 在實(shí)例上調(diào)用指定的方法
- 銷毀這個(gè)實(shí)例
相當(dāng)于解釋器完成了如下代碼
var s1 = new String("some text");
var s2 = s1.substring(2);
s1 = null;
注
:引用類型與基本包裝類型的主要區(qū)別就是對(duì)象的生存期。使用new操作符創(chuàng)建的引用類型的實(shí)例,在執(zhí)行流離開當(dāng)前作用域之前都一直保存在內(nèi)存中许溅。而自動(dòng)創(chuàng)建的基本包裝類型的對(duì)象瓤鼻,則只存在于一行代碼的執(zhí)行瞬間,然后立即被銷毀
- Number
Number類型也重寫了valueOf()
贤重、toLocaleString()
和toString()
方法
重寫后的valueOf()
方法返回對(duì)象表示的基本類型的數(shù)值茬祷,另外兩個(gè)方法則返回字符串形式的數(shù)值-
toString()
可以傳遞一個(gè)表示基數(shù)的參數(shù),告訴它返回進(jìn)制數(shù)值的字符串形式var num = 10; console.log(num.toString());//10 console.log(num.toString(2));//1010 console.log(num.toString(8));//12 console.log(num.toString(10));//10 console.log(num.toString(16));//a
-
toFixed()
該方法會(huì)按照指定的小數(shù)位返回?cái)?shù)值的字符串表示var num = 10; console.log(num.toFixed(2));//10.00
-
toExponential()
該方法返回以指數(shù)表示法表示的數(shù)值的字符串形式var num = 10; console.log(num.toExponential(1));//1.0e+1
-
toPrecision()
該方法可能會(huì)返回固定大小(fixed)格式并蝗,也有可能返回指數(shù)(exponential)格式var num = 99; console.log(num.toPrecision(1));//1e+2 console.log(num.toPrecision(2));//99 console.log(num.toPrecision(3));//99.0
-
- String類型
String類型是字符串的對(duì)象包裝類型祭犯,可以按照如下方式創(chuàng)建var str1 = "hello world"; var str2 = new String("hello world");
-
length
屬性
該屬性用于表示字符串中包含多個(gè)字符串 - 字符方法
兩個(gè)用于訪問字符串中特定字符的方法是charAt()
和charCodeAt()
。這兩個(gè)方法都接收一個(gè)參數(shù)滚停,基于0的字符位置var str = "hello world"; console.log(str.charAt(1));//e console.log(str.charCodeAt(1));//101
- 字符串操作方法
-
concat()
用于將一個(gè)或多個(gè)字符串拼接起來沃粗,返回拼接得到的新字符串var str = "hello".concat(" world","!","!"); console.log(str);//hello world!!
slice()
、substr()
和substring()
键畴。這三個(gè)方法都會(huì)返回被操作字符串的一個(gè)子字符串-
slice()
第一個(gè)參數(shù)指定字符串開始的位置陪每,第二個(gè)參數(shù)指定子字符串最后一個(gè)字符后面的位置var str = "hello world"; console.log(str.slice(3));//lo world console.log(str.slice(3,7));//lo w
-
substring()
第一個(gè)參數(shù)指定字符串開始的位置,第二個(gè)參數(shù)指定子字符串最后一個(gè)字符后面的位置var str = "hello world"; console.log(str.substring(3));//lo world console.log(str.substring(3,7));//lo w
-
substr()
第一個(gè)參數(shù)指定字符串開始的位置镰吵,第二個(gè)參數(shù)指定返回字符的個(gè)數(shù)var str = "hello world"; console.log(str.substr(3));//lo world console.log(str.substr(3,7));//lo worl
-
- 字符串位置方法
indexOf()
和lastIndexOf()
檩禾。這兩個(gè)方法都是從一個(gè)字符串中搜素給定的子字符串,然后返回子字符串的位置(如果沒有返回-1)-
indexOf()
從頭開始搜索var str = "hello world"; console.log(str.indexOf("o"));//4
-
lastIndexOf()
從末尾開始搜索var str = "hello world"; console.log(str.lastIndexOf("o"));//7
-
-
trim()
方法
該方法會(huì)創(chuàng)建一個(gè)字符串的副本疤祭,刪除前置及后綴的所有空格盼产,然后返回結(jié)果var str = " hello world "; console.log(str.trim());//hello world console.log(str);//" hello world "
- 字符串大小寫轉(zhuǎn)換方法
toUpperCase()
和toLowerCase()
是兩個(gè)經(jīng)典的方法,而toLocaleUpperCase()
和toLocaleLowerCase()
方法則是針對(duì)特定地區(qū)的實(shí)現(xiàn)勺馆,一些地區(qū)可能會(huì)不同var str = "Hello World"; console.log(str.toUpperCase());//HELLO WORLD console.log(str.toLocaleUpperCase());//HELLO WORLD console.log(str.toLowerCase());//hello world console.log(str.toLocaleLowerCase());//hello world
- 字符串正則表達(dá)式方法
-
match()
var text = "cat, bat, sat, fat"; var matchs = text.match(/.at/); console.log(matchs.index);//0
-
search()
search()
方法返回字符串第一個(gè)匹配項(xiàng)的索引戏售,如果沒有返回-1,該方法始終是從開頭查找var text = "cat, bat, sat, fat"; var pos = text.search(/.at/); console.log(pos);//0
-
-
localeCompare()
該方法用于比較字符串var str = "yellow"; console.log(str.localeCompare("yellow"));//0 console.log(str.localeCompare("brick"));//1 console.log(str.localeCompare("zoo"));//-1
-
單體內(nèi)置對(duì)象
-
global
對(duì)象
不屬于任何對(duì)象的屬性和方法,最終都是它的屬性和方法草穆。事實(shí)上灌灾,也沒有全局變量或者全局函數(shù);所有在全局定義的屬性和函數(shù)悲柱,都是global
對(duì)象的屬性- URL編碼
encodeURI()
和encodeURIComponent()
方法可以對(duì)URI進(jìn)行編碼锋喜,以便發(fā)送給瀏覽器。對(duì)整個(gè)URI使用encodeURI()
豌鸡。而對(duì)附加在現(xiàn)有URI后面的字符串使用encodeURIComponent()
與這兩個(gè)方法對(duì)應(yīng)的是//http://www.baidu.com/index%20mm.html console.log(encodeURI("http://www.baidu.com/index mm.html")); //http%3A%2F%2Fwww.baidu.com%2Findex%20mm.html%2F%E9%92%A2%E9%93%81 console.log(encodeURIComponent("http://www.baidu.com/index mm.html/鋼鐵"));
decodeURI()
和decodeURIComponent()
decodeURI()
只能對(duì)使用encodeURI()
替換的字符進(jìn)行解碼嘿般,同樣decodeURIComponent()
能夠解碼使用encodeURIComponent()
編碼的所有字符//http%3A%2F%2Fwww.baidu.com%2Findex mm.html%2F鋼鐵 console.log(decodeURI("http%3A%2F%2Fwww.baidu.com%2Findex%20mm.html%2F%E9%92%A2%E9%93%81")); //http://www.baidu.com/index mm.html/鋼鐵 console.log(decodeURIComponent("http%3A%2F%2Fwww.baidu.com%2Findex%20mm.html%2F%E9%92%A2%E9%93%81"));
-
eval()
方法
該方法就像是一個(gè)完整的ECMAScript解析器,它只接受一個(gè)參數(shù)涯冠,即要執(zhí)行的ECMAScript(或JS)字符串
通過eval("console.log(\"hello world\");"); console.log("hello world");
eval()
執(zhí)行的代碼被認(rèn)為是包含該次調(diào)用的執(zhí)行環(huán)境一部分炉奴,因此被執(zhí)行的代碼具有與該執(zhí)行環(huán)境相同的作用域鏈。
注
:嚴(yán)格模式下蛇更,在外部訪問不到eval()
中創(chuàng)建的任何變量或者函數(shù)瞻赶,而且為eval
賦值也會(huì)導(dǎo)致錯(cuò)誤
- URL編碼
-
Math
對(duì)象
Math
對(duì)象提供的計(jì)算功能執(zhí)行起來要快得多-
min()
和max()
min()
和max()
用于確定一組數(shù)值里面的最大和最小值赛糟。這兩個(gè)方法都可以接收任意多個(gè)數(shù)值的參數(shù)console.log(Math.max(3,52,32,16));//52 console.log(Math.min(3,52,32,16));//3
- 舍入方法
將小數(shù)值舍入整數(shù)有幾個(gè)方法Math.ceil()
,Math.floor()
砸逊,Math.round()
-
Math.ceil()
執(zhí)行向上舍入 -
Math.floor()
執(zhí)行向下舍入 -
Math.round()
執(zhí)行標(biāo)準(zhǔn)舍入虑灰,四舍五入
console.log(Math.ceil(29.4));//30 console.log(Math.floor(29.4));//29 console.log(Math.round(29.4));//29
-
-
random()
方法
Math.random()
方法返回大于等于0小于1的一個(gè)隨機(jī)數(shù)
可以套用下面的公式,獲得隨機(jī)值
例如獲得2到9則為:值 = Math.floor(Math.random() * 可能值的總數(shù) + 第一個(gè)可能的值)
num = Math.floor(Math.random() * 9 + 2);
-