f1(); console.log(c); console.log(b); console.log(a); function f1() { var a = b = c = 9; //var a = 9;b=9;c=9 console.log(a); console.log(b); console.log(c); }
内置对象
1、构造函数语法:
1 2 3 4 5
function 构造函数名(值,) { this.属性 = 值; this.方法 = function(形参) {} }//定义 new 构造函数名();//调用
2、遍历对象
1 2 3 4
for(var k in obj) { console.log(k);//属性名 console.log(obj[k]);//属性值 }
3、var date = new Date(‘20222-7-2 8:8:8’); 4、var date1 = +new Date(); // 返回距离 1970.1.1 的总毫秒数 H5 语法:Date.now() 案例:倒计时
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
function countDown(time) { var now = +new Date(); var input = +new Date(time); var t = (input - now) / 1000; var day = parseInt(t / 60 / 60 / 24); day = day == 0 ? '0' + day : day; var hour = parseInt(t / 60 / 60 % 24); hour = hour == 0 ? '0' + hour : hour; var min = parseInt(t / 60 % 60); min = min == 0 ? '0' + min : min; var sec = parseInt(t % 60); sec = sec == 0 ? '0' + sec : sec; var res = day + '天:' + hour + '小时:' + min + '分钟:' + sec+'秒'; return res; } console.log(countDown('2022-7-2 18:0:0')); 数组对象
function checkDouble(arr) { var newArr = []; for (var i = 0; i < arr.length; i++) { if (newArr.indexOf(arr[i]) == -1) newArr.push(arr[i]); } return newArr; } console.log(checkDouble(arr));
var str = 'abcdoedefgoefdoeafao'; var index = str.indexOf('o'); var cnt = 0; while (index != -1) { cnt++; console.log(index); index = str.indexOf('o', index + 1); } console.log(cnt);
var str = 'abcdoodfegoefdeafaeze'; var obj = {}; for (var i = 0; i < str.length; i++) { var chars = str.charAt(i) if (obj[chars]) { obj[chars]++; } else { obj[chars] = 1; } } var max = 0; var c = ''; for (k in obj) { if (obj[k] > max) { max = obj[k]; c = k; }