JavaScript内置对象

Global对象的内置方法

        var box = '//Lee 李';
	alert(encodeURI(box));          //返回//Lee%20%E6%9D%8E  只编码了中文
	
	var box = '//Lee 李';
	alert(encodeURIComponent(box));      //返回%2F%2FLee%20%E6%9D%8E 特殊字符和中文都编码了
	
	PS:因为encodeURIComponent()编码比encodeURI编码更彻底,因此前者的使用频率要高一些
        var box = '//Lee 李';
	alert(decodeURI(encodeURI(box)));           //返回//Lee 李 还原
	var box = '//Lee 李';
	alert(decodeURIComponent(encodeURIComponent(box)));   //返回//Lee 李 还原

	
    'var box = 100';
    //alert(box);                         //box is not defined
    eval('var box = 100')                 //字符串解析器,解析了字符串代码
alert(box);                           //100


eval('function box(){ return 123}');	//字符串解析器
alert(box()); //返回123
ps:eval()功能非常强大,但也非常危险。使用的时候要十分谨慎。特别是在用户输入数据的情况下,非常有可能导致程序的安全性,比如代码注入等等。

Math对象的属性和方法

alert(Math.E); //返回2.718281828459045
alert(Math.PI);//3.141592653589793

alert(Math.min(2,5,8,4,9,2,0,1)); //求最小值 返回0
alert(Math.max(2,5,8,4,9,2,0,1)); //求最大值 返回9

alert(Math.ceil(25.9)); //向上舍入 返回26
alert(Math.ceil(25.5)); //向上舍入 返回26
alert(Math.ceil(25.1)); //向上舍入 返回26

alert(Math.floor(25.9)); //向下舍入 返回25
alert(Math.floor(25.5)); //向下舍入 返回25
alert(Math.floor(25.1)); //向下舍入 返回25

alert(Math.round(25.9)); //标准舍入,四舍五入 返回26
alert(Math.round(25.5)); //标准舍入,四舍五入 返回26
alert(Math.round(25.1)); //标准舍入,四舍五入 返回25

alert(Math.random());//返回0到1之间的随机数,不包括0和1

for(var i=0; i<10; i++){
	document.write(Math.random());
	document.write('<br/>');
	
}

for(var i=0; i<10; i++){
	document.write(Math.floor(Math.random()*10+1));//随机产生1-10之间的任意数
	document.write('<br/>');
	
}

function select(start, end){                          //随机产生start到end之间的任意数    
	var total = end - start + 1;
	return Math.floor(Math.random()*total + start);
}
 for(var i=0; i<10; i++){
	 document.write(select(5,11));
	 document.write('<br/>');
 }
 
 
alert(Math.abs(-5));//返回绝对值5
alert(Math.sqrt(9));//返回9的平方根3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值