JS内置对象
2017-04-21 18:24
183 查看
一,Global对象
Global(全局)对象是ECMAScript中一个特别的对象,因为这个对象是不存在的。在ECMAScript中不属于任何其他的属性和方法,都属于它的属性和方法。所以,事实上,并不存在全局变量和全局函数;所有在全局作用域定义的变量和函数,都是Global对象的属性和方法。
ECMAScript没有定义怎么调用Global对象,所以,Global.属性或者Global.方法()都是无效的。
(Web浏览器将Global作为window对象的一部分加以实现)
Global对象有一些内置的属性和方法。
var box = '//Lee 李';
//alert(encodeURI(box)); //只编码中文
//alert(encodeURIComponent(box)); //编码了特殊字符和中文
var a = encodeURI(box);
var b = encodeURIComponent(box);
alert(decodeURI(a)); //decodeURI解码
alert(decodeURIComponent(box)); //decodeURIComponent解码
eval()方法主要担当一个字符串解析器的作用,他只接受一个参数,而这个参数就是要执行的JavaScript代码的字符串。
eval('var box = 100'); //解析了字符串代码
alert(box);
eval('alert(100)');
eval('function box(){return 123}'); //也可以解析函数
alert(box());
二.Math对象
ECMAScript还为保存数学公式和信息提供了一个对象,即Math对象。与我们在JavaScript直接编写计算功能相比,Math对象提供的计算功能执行起来要快得多。
alert(Math.E);
alert(Math.PI);
//min()和max()方法
alert(Math.min(2,5,8,4,9,2,0,1)); //最小值
alert(Math.max(2,5,8,4,9,2,0,1)); //最大值
//舍入方法
//向上舍入 将数值向上舍入为最接近的整数
alert(Math.ceil(25.9));
alert(Math.ceil(25.5));
alert(Math.ceil(25.1));
//向下舍入 将数值向下舍入为最接近的整数
alert(Math.floor(25.9));
alert(Math.floor(25.5));
alert(Math.floor(25.1));
//标准舍入即四舍五入 将数值四舍五入为最接近的整数
alert(Math.round(25.9));
alert(Math.round(25.5));
alert(Math.round(25.1));
//Math.random()方法返回介于0到1之间一个随机数,不包括0和1。如果想大于这个范围的话,可以套用一下公式:
值 = Math.floor(Math.random()*总数+第一个值)
alert(Math.random());
for(var i=0; i<10; i++)
{
document.write(Math.random());
document.write('<br />');
}
alert(Math.floor(Math.random()*10+1)); //随机产生1-10之间的任意数
for(var i=0; i<10; i++)
{
document.write(Math.floor(Math.random()*10+1));
document.write('<br />');
}
//返回5-10之间
for(var i=0; i<10; i++)
{
document.write(Math.floor(Math.random()*10+5)); //范围5-14之间 10+5-1=14(5-14)
document.write('<br />');
}
//如果想5-10之间,10-5+1=6
for(var i=0; i<10; i++)
{
document.write(Math.floor(Math.random()*6+5)); //范围5-10之间 6+5-1=10(5-10)
document.write('<br />
');
}
//上述方法太麻烦,还需要计算开始值。可以编写一个函数用于计算开始值
function select(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,10));
document.write('<br />
');
}
alert(Math.abs(-5)); //返回num的绝对值
alert(Math.sqrt(9)); //返回num的平方根
Global(全局)对象是ECMAScript中一个特别的对象,因为这个对象是不存在的。在ECMAScript中不属于任何其他的属性和方法,都属于它的属性和方法。所以,事实上,并不存在全局变量和全局函数;所有在全局作用域定义的变量和函数,都是Global对象的属性和方法。
ECMAScript没有定义怎么调用Global对象,所以,Global.属性或者Global.方法()都是无效的。
(Web浏览器将Global作为window对象的一部分加以实现)
Global对象有一些内置的属性和方法。
var box = '//Lee 李';
//alert(encodeURI(box)); //只编码中文
//alert(encodeURIComponent(box)); //编码了特殊字符和中文
var a = encodeURI(box);
var b = encodeURIComponent(box);
alert(decodeURI(a)); //decodeURI解码
alert(decodeURIComponent(box)); //decodeURIComponent解码
eval()方法主要担当一个字符串解析器的作用,他只接受一个参数,而这个参数就是要执行的JavaScript代码的字符串。
eval('var box = 100'); //解析了字符串代码
alert(box);
eval('alert(100)');
eval('function box(){return 123}'); //也可以解析函数
alert(box());
二.Math对象
ECMAScript还为保存数学公式和信息提供了一个对象,即Math对象。与我们在JavaScript直接编写计算功能相比,Math对象提供的计算功能执行起来要快得多。
alert(Math.E);
alert(Math.PI);
//min()和max()方法
alert(Math.min(2,5,8,4,9,2,0,1)); //最小值
alert(Math.max(2,5,8,4,9,2,0,1)); //最大值
//舍入方法
//向上舍入 将数值向上舍入为最接近的整数
alert(Math.ceil(25.9));
alert(Math.ceil(25.5));
alert(Math.ceil(25.1));
//向下舍入 将数值向下舍入为最接近的整数
alert(Math.floor(25.9));
alert(Math.floor(25.5));
alert(Math.floor(25.1));
//标准舍入即四舍五入 将数值四舍五入为最接近的整数
alert(Math.round(25.9));
alert(Math.round(25.5));
alert(Math.round(25.1));
//Math.random()方法返回介于0到1之间一个随机数,不包括0和1。如果想大于这个范围的话,可以套用一下公式:
值 = Math.floor(Math.random()*总数+第一个值)
alert(Math.random());
for(var i=0; i<10; i++)
{
document.write(Math.random());
document.write('<br />');
}
alert(Math.floor(Math.random()*10+1)); //随机产生1-10之间的任意数
for(var i=0; i<10; i++)
{
document.write(Math.floor(Math.random()*10+1));
document.write('<br />');
}
//返回5-10之间
for(var i=0; i<10; i++)
{
document.write(Math.floor(Math.random()*10+5)); //范围5-14之间 10+5-1=14(5-14)
document.write('<br />');
}
//如果想5-10之间,10-5+1=6
for(var i=0; i<10; i++)
{
document.write(Math.floor(Math.random()*6+5)); //范围5-10之间 6+5-1=10(5-10)
document.write('<br />
');
}
//上述方法太麻烦,还需要计算开始值。可以编写一个函数用于计算开始值
function select(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,10));
document.write('<br />
');
}
alert(Math.abs(-5)); //返回num的绝对值
alert(Math.sqrt(9)); //返回num的平方根
相关文章推荐
- js的function, javascript内置对象
- 学习JavaScript 的必备 (一),让您对js的 function, javascript内置对象,this概念及之间的关系不再迷惑。(希望能置为推荐篇,为更多的js初学者关注)
- JavaScript第四章知识点总结——JS内置对象 Array Date Math
- 常用的js内置对象
- js中的history内置对象在各浏览器中的用法:
- JS "portotype" (内置对象原型改写)
- js中内置有对象
- js的内置对象
- JavaScript高级程序设计 阅读笔记(十二) js内置对象Math
- js的内置对象
- 学习JavaScript 的必备 (一),让您对js的 function, javascript内置对象,this概念及之间的关系不再迷惑。(希望能置为推荐篇,为更多的js初学者关注)
- 让您对js的 function, javascript内置对象,this概念及之间的关系不再迷惑
- 有关js的arguments内置对象的用法
- js函数与内置对象 第一天
- javascript学习笔记(八) js内置对象
- js的 function, javascript内置对象,this概念及之间的关系不再迷惑。
- 让您对js的 function, javascript内置对象,this概念及之间的关系不再迷惑
- js内置对象 学习笔记
- 笔记:js内置对象及自定义对象
- 用js内置对象XMLHttpRequest 来用ajax