Cirry's Blog

内置对象Global,Math

Jan 4, 2017
技术 js
2分钟
204字

印象笔记迁移

global对象

var box = '//dog学';
var a = encodeURI(box);
var b = encodeURIcomponent(box); //%2F%2Fdog%E5%AD%A6
alert(a); // //dog%E5%AD%A6
alert(b); // %2F%2Fdog%E5%AD%A6
alert(decodeURI(a)); // //dog学
alert(decodeURIComponent(b)); // //dog学
eval('var box = 100');
alert(box);
eval(alert(100)); //100
alert(eval(alert(100))); //100,undefined 第一个打印100,第二个打印eval的返回值
eval('function box(){return 123}');
alert(box()); //123

Math对象

alert(Math.E); //2.718281828459045
alert(math.PI); //3.141592653589793
alert(Math.min(2,5,8,0)); //0
alert(Math.max(2,5,8,0)); //8
alert(Math.abs(-5)); //5
alert(Math.sqrt(9)); //3
alert(Math.ceil(25.1)); //26
alert(Math.ceil(25.5)); //26
alert(Math.ceil(25.9)); //26,向上舍入
alert(Math.floor(25.1)); //25
alert(Math.floor(25.5)); //25
alert(Math.floor(25.9)); //25,向下舍入
3 collapsed lines
alert(Math.round(25.1)); //25
alert(Math.round(25.5)); //26
alert(Math.round(25.9)); //26,标准的四舍五入

Math.random()方法

= Math.floor(Math.random() *总数 + 第一个值)

for (var i = 0; i < 10; i++) {
document.write(Math.floor(Math.random() * 10 + 1));
document.write('<br />');
} //1-10之间
for (var i = 0; i < 10; i++) {
document.write(Math.floor(Math.random() * 6 + 5));
document.write('<br />');
} //5-10之间
function select(start, end) {
var total = end - start + 1;
return Math.floor(Math.random() * total + start);
}
4 collapsed lines
for (var i = 0; i < 10; i++) {
document.write(select(5, 10));
document.write('<br />');
} //5-10之间
本文标题:内置对象Global,Math
文章作者:Cirry
发布时间:Jan 4, 2017
感谢大佬送来的咖啡☕
alipayQRCode
wechatQRCode
总访问量
总访客数人次