Cirry's Blog

内置对象Global,Math

2017-01-04
技术
js
最后更新:2024-04-02
2分钟
205字

印象笔记迁移

global对象

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

Math对象

1
alert(Math.E); //2.718281828459045
2
alert(math.PI); //3.141592653589793
3
alert(Math.min(2,5,8,0)); //0
4
alert(Math.max(2,5,8,0)); //8
5
alert(Math.abs(-5)); //5
6
alert(Math.sqrt(9)); //3
7
8
alert(Math.ceil(25.1)); //26
9
alert(Math.ceil(25.5)); //26
10
alert(Math.ceil(25.9)); //26,向上舍入
11
12
alert(Math.floor(25.1)); //25
13
alert(Math.floor(25.5)); //25
14
alert(Math.floor(25.9)); //25,向下舍入
15
3 collapsed lines
16
alert(Math.round(25.1)); //25
17
alert(Math.round(25.5)); //26
18
alert(Math.round(25.9)); //26,标准的四舍五入

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

1
for (var i = 0; i < 10; i++) {
2
document.write(Math.floor(Math.random() * 10 + 1));
3
document.write('<br />');
4
} //1-10之间
5
6
for (var i = 0; i < 10; i++) {
7
document.write(Math.floor(Math.random() * 6 + 5));
8
document.write('<br />');
9
} //5-10之间
10
11
function select(start, end) {
12
var total = end - start + 1;
13
return Math.floor(Math.random() * total + start);
14
}
15
4 collapsed lines
16
for (var i = 0; i < 10; i++) {
17
document.write(select(5, 10));
18
document.write('<br />');
19
} //5-10之间
本文标题:内置对象Global,Math
文章作者:Cirry
发布时间:2017-01-04
版权声明:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
感谢大佬送来的咖啡☕
alipayQRCode
wechatQRCode