前端工作常用JavaScript记录

判断是否为空
 
if(!obj==false) //true 为空 注意:0特殊当作false

 判断

//条件?真结果:假结果
var test = a>0?a:-a

 

//条件1?真结果1:(条件2?真结果2:(条件3:真结果3?假结果3))
var test = x>0?1:(x=0?-1:2)

 

多条件的三目运算

实例:根据学生成绩判定ABCD四个等级

var result = (sc<0 || sc>100) ?("分数无效"):sc>=90?("A"):sc>=80?("B"):   sc>=60?("C"):("D");

 

 

字符串
 
split() 方法用于把一个字符串分割成字符串数组。
"2:3:4:5".split(":")   //将返回["2", "3", "4", "5"]

 

indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。

<script type="text/javascript"
var str="Hello world!"
console.log(str.indexOf("Hello")) //0
console.log(str.indexOf("World")) //-1 不存在
console.log(str.indexOf("world")) //6
</script>

 

 trim() 方法用于删除字符串的头尾空白符,空白符包括:空格、制表符 tab、换行符等其他空白符等。

var str = "       Runoob        ";
alert(str.trim());

输出结果:Runoob

 

 

数组

splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。

<script type="text/javascript">

var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
arr.splice(2,3,"William")
document.write(arr)

</script>

George,John,Thomas,James,Adrew,Martin
George,John,William,Martin
 
 includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
 

join() 方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr.join())

输出:George,John,Thomas

 

 

循环
 
 for
for(var i=0;i<10;i++){
    console.log(i);
}
 
for- in语句用于对数组或者对象的属性进行循环操作
let obj={'name':'programmer','age':'22','height':'180'};
for(let i in obj){
    console.log(i,obj[i])
}

//name programmer
//age 22
//height 180

 

for- of

JavaScript 原有的for-in循环,只能获得对象的键名,不能直接获取键值。ES6 提供for...of循环,允许遍历获得键值

 

//数组
var arr = ['a', 'b', 'c', 'd'];

for (let a in arr) {
      console.log(a); // 0 1 2 3
}
        
for (let a of arr) {
       console.log(a); // a b c d
}

// 字符串
var str = "hello";
for (let s of str) {
       console.log(s); // h e l l o
}

 

循环控制语句

 

break:跳出本层循环,继续执行循环后面的语句
continue:跳过本次循环剩余的代码,继续执行下一次循环。

 
日期
 
 Date转字符串
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子: 
// (new Date()).Format("yyyy-MM-dd HH:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d H:m:s.S")      ==> 2006-7-2 8:9:4.18
//HH 24小时制 hh12小时制 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

调用: 
var time1 = new Date().Format("yyyy-MM-dd");
var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");  

 

//日期格式化
                        var da = res.data.data.createTime;
                        da = new Date(da);
                        var year = da.getFullYear()
                        var month = da.getMonth() + 1;
                        if (month < 10) {
                            month = '0' + month;
                        }
                        var date = da.getDate();
                        if (date < 10) {
                            date = '0' + date;
                        }
                        res.data.data.createTime = [year, month, date].join('-');

 

 

 字符串转Date

var time1=‘2016-01-01 17:22:37’;
var date=new Date(time1.replace(/-/g, '/'));  //开始时间
console.log(data);//js data
var time2=date.getTime();
console.log(time2);//毫秒数

 

周几

var day = new Date(value).getDay(),text = "";
    switch (day) {
       case 0:
        text = "星期日";
        break;
       case 1:
        text = "星期一";
        break;
       case 2:
        text = "星期二";
        break;
       case 3:
        text = "星期三";
        break;
       case 4:
        text = "星期四";
        break;
       case 5:
        text = "星期五";
        break;
       case 6:
        text = "星期六";
        break;
     }
 return text;

 

 
JSON
 
JSON.parse() 方法用于将一个 JSON 字符串转换为对象。
JSON.parse()
 
JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。
JSON.stringify()

 

JS延时,轮询

clearTimeout(this.timer);  //清除延迟执行 
 
this.timer = setTimeout(()=>{   //设置延迟执行
    console.log('ok');
},1000);

clearInterval(this.fortimer);  //清除轮询执行 
 
this.fortimer = setInterval(()=>{   //设置轮询执行
    console.log('ok');
},1000);

 

 

 

posted @ 2020-10-09 23:41  熊大大001(前端开发)  阅读(125)  评论(0)    收藏  举报