zoukankan      html  css  js  c++  java
  • javascript简写精练

    一、算术运算符
    var n = 5,v;
    1.n = n*5;
    
    2.n*=5; 同 n = n*5
    
    二、条件判断
    var b = true;
    1.if (!false) {
        alert('true');
    }
    
    2.!b || alert('true');
    
    用"||"的情况下,第一个条件true,不检测第二个直接返回true.第一个条件
    
    false,会执行第二个条件检测
    
    3.b && alert('true'); 
    
    用"&&"的情况下,第一个条件true,还会检测第二个条件。第一个条件false,
    
    直接返回false退出。
    
    4.alert(!b ? 'false' : 'true'); 
    
    三、对象转数组
    var arr = [].slice.call({ length: 2, 0: "a", 1: "b" })
    或
    var arr = Array.prototype.slice.call({ length: 2, 0: "a", 1: "b" })
    结果:
    alert(arr[1]); //["a","b"]
    
    length不能去掉,指写slice的宽度
    
    四、随机码
    Math.random().toString(16).substring(2); //14位   
    Math.random().toString(36).substring(2); //11位 
    
    五、合并数组
    var arr = [1, 2, 3];
    Array.prototype.push.apply(arr, [4, 5, 6]);
    alert(arr[1]); //[1, 2, 3, 4, 5, 6]
    
    六、交换值
    var a = 0, b = 1;
    a = [b, b = a][0];
    alert(a); //a = 1, b = 0
    
    七、删除数组元素
    var a = [1,2,3,4,5];   
    a.splice(3,1);
    
    八、快速取数组最大和最小值
    Math.max.apply(Math, [1,2,3]) //3   
    Math.min.apply(Math, [1,2,3]) //1  
    
    九、日期转数值
    var d = +new Date(); //1295698416792    时间戳:Math.round(new Date().getTime()/1000) 
    十、字面量
    var a={} 同 var a=new Object();
    var b=[] 同 var b=new Array();
    
    十一、非空对象与非空字符串
    if(x==null)或if (typeof (x) == 'undefined')可以简写为if(!x)
    
  • 相关阅读:
    区块链
    区块链
    区块链
    区块链
    区块链 – 介绍
    区块链 教程
    Matplotlib 直方图
    Matplotlib 饼图
    Matplotlib 柱状图
    Matplotlib 多个图形
  • 原文地址:https://www.cnblogs.com/sntetwt/p/3383900.html
Copyright © 2011-2022 走看看