155.练习题商场小玩意你能停到100么?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<button id="to">开始</button>
<button id="st">停止</button>
<script type="text/javascript">
var add;
function go(){
clearInterval(add);
num=0;
add=setInterval(function(){
num+=1;
console.log(num);
},5);
};
function stopGo(){
clearInterval(add);
};
to.onclick=go;
st.onclick=stopGo;
</script>
</body>
</html>
156.Number对象和NaN
一.Number() *Number方法可以把一个字符串尽最大的努力变成数字,如果无法转换成数字,则输出NaN(not a number)
二.转化例子
Number(“123”) 输出:123
Number(true) 输出:1
Number(false) 输出:0
Number(“true”) 输出:NaN
Number(“null”) 输出:0
Number(“undefined”) 输出:0
Number(“ding”) 输出:NaN
157.递增和递减
一.递增
var a=0;
a+=1; *a=a+1;
var a=0;
a++; *a=Number(a)+1;a会被尽量转换成数字,如果无法转换,则会输出NaN。NaN+任何数字都是NaN.
var a=0;
++a; *与a++执行相同的运算规则,区别:a++,a的变量为原先的变量;++a,a的变量为运算后的新值。
158.JS的计算BUG
JS有计算BUG主要体现在小数点的第16位
例:
alert(1.1+2.2); 输出:3.3000000000000003
解决计算bug 的方法:在计算时运算的数值乘以10倍值消除小数点后再运算,得出的结果再除以10的倍值。
alert((1.1*10+2.2*10)/10);
159.联系题-你能点到10么?(小数点版本)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<button id="to">开始</button>
<button id="st">停止</button>
<script type="text/javascript">
var add;
function go(){
clearInterval(add);
num=0;
add=setInterval(function(){
num=(num*10+1)/10;
console.log(num);
},50);
};
function stopGo(){
clearInterval(add);
};
to.onclick=go;
st.onclick=stopGo;
</script>
</body>
</html>
160.字符串对象和字符串拼接
一.String() *和Number()方法比较像,不管放入任何参数均转化成字符串
二.字符串拼接
字符串+任何东西都会变成字符串拼接
console.log(“1”+”b”); *输出1b
console.log(“1”+”10”); *输出110
console.log(“1”+function(){}); *输出1function(){};
161.字符串类型的基本运算
一.除了+法运算,JS会把所有的字符串进行隐式转换成为数字再进行运算,如不能转换只能输出NaN。但是只要涉及+,当参数是字符串时就会变成字符串拼接。
例如:console.log(“2”+5*2); *输出210
二.其它运算
<,>,<=,>= 均显示boll值。
字符串比较:
console.log(“51”>”451”) *输出false,字符串只比较第一位。
console.log(“一”>”二”) *输出 false,用code源码进行比较。
console.log(“1”==1) *输出true,触发隐式转换。
=== 绝对等于判断,隐式转换不再起作用,最精准的判断。
155-161 李游前端精品课程笔记
最新推荐文章于 2024-06-02 21:27:33 发布