两个单体内置对象_Global和Math
內(nèi)置對(duì)象:不依賴于宿主對(duì)象,在程序執(zhí)行之前就已經(jīng)存在。例如Object,Array和String。
今天主要學(xué)習(xí)另外兩個(gè)單體內(nèi)置對(duì)象,Global和Math。
(一)Global 對(duì)象
URI編碼方法:
encodeURI()方法可以對(duì)URI進(jìn)行編碼,主要用于整個(gè)URI。不會(huì)對(duì)本身屬于URI的特殊字符進(jìn)行編碼,例如冒號(hào),正斜杠,問(wèn)號(hào)和井號(hào)等。
encodeURIComponent()方法也是對(duì)URI進(jìn)行編碼,主要用于一段URI。會(huì)對(duì)任何非標(biāo)準(zhǔn)字符進(jìn)行編碼。
例如:var uri="http://www.wrax.com/illegal value.html#start";
alert(encodeURI(uri)); //"http://www.wrax.com//illegal%20value.html#start"?????? (這里的空格變成了%20)
alert(encodeURIComponent(uri));//"http%3A%2F%2Fwww.wrax.com%2Fillegal%20value.html%23start"????? (這里使用了對(duì)應(yīng)編碼替換所有非字母數(shù)字的字符)
?
decodeURI()方法可以對(duì)encodeURI()替換的字符進(jìn)行解碼。
decodeURIComponent()方法能夠解碼encodeURIComponent()方法編碼的所有字符。
例如:var uri="http%3A%2F%2Fwww.wrax.com%2Fillegal%20value.html%23start"
alert(decodeURI(uri)); //"http%3A%2F%2Fwww.wrax.com%2Fillegal value.html%23start"????? (這里只有%20替換回空格)
alert(decodeURIComponent(uri));//"http://www.wrax.com//illegal value.html#start"?? (所有的特殊字符的編碼都替換回原來(lái)的字符)
?
eval()方法值接收一個(gè)參數(shù),可以把一個(gè)字符串當(dāng)作JS表達(dá)式去執(zhí)行它。
例如:
var num="1+1";
var num1=eval("1+1");
alert(num);// 1+1
alert(num1);//2
?
(二)Math對(duì)象
min()和max()方法用于確定一組數(shù)值中最小值和最大值,可以接收多個(gè)數(shù)值參數(shù)。
var min=Math.min(1,6,10,22,55);
alert(min);//1
var max=Math.max(1,6,10,22,55);
alert(max);//55
如果要找到數(shù)組的最大值或最小值,可以使用apply()方法:
var num=[1,2,3,4,5];
var max=Math.max.apply(Math,num);
alert(max);//5
?
舍入方法:
Math.ceil()方法:執(zhí)行向上舍入最接近的整數(shù)。
Math.floor()方法:執(zhí)行向下舍入最接近的整數(shù)。
Math.round()方法:執(zhí)行標(biāo)準(zhǔn)的四舍五入。
?
random()方法返回大于等于0小于1的一個(gè)隨機(jī)數(shù)。可以利用random()方法從某個(gè)整數(shù)范圍內(nèi)選擇一個(gè)值??
值=Math.random()*可能值的總數(shù)+第一個(gè)可能值
function selectFrom(lowerValue,upperValue){
??????????? var choice=upperValue-lowerValue+1;
???????????? return Math.floor(Math.random()*choice+lowerValue);
}
var num=selectFrom(1,10);
alert(num); //1到10之間的隨機(jī)數(shù)
?
轉(zhuǎn)載于:https://www.cnblogs.com/guoxihong/p/5374212.html
總結(jié)
以上是生活随笔為你收集整理的两个单体内置对象_Global和Math的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 创建交互式shell脚本对话框
- 下一篇: Log4j初步配置