JavaScript——小技巧/骚东西

1、通过递归来求阶乘

function factorial(n) { return (n > 1) ? n * f(n - 1) : n}

2、交换值

1. var temp = a; a = b; b = temp; (传统,但需要借助临时变量)
2. a ^= b; b ^= a; a ^= b; (需要两个整数)
3. b = [a, a = b][0] (借助数组)
4. [a, b] = [b, a]; (ES6,解构赋值)
5. a = a + b; b = a - b; a = a - b; (小学奥赛题)

3、数组克隆

arr.slice(0)
arrCopy = JSON.parse(JSON.stringify(arr))  //深拷贝

4、数组最小值

function minArr(arr) { return Math.min.apply(null, arr)}

5、数组最大值

function maxArr(arr) { return Math.max.apply(null, arr)}  
//或者使用ES6中的拓展运算符Math.max(...[1,2,3,4,5])

6、骚东西

console.log(typeof NaN);    //number
console.log(9999999999999999);  //10000000000000000  
//Javascript里的浮点数是64位双精度浮点数,能够精确表示的最大整数大概在17位的十进制数,这个临界值是多少我没有具体研究。大于这个临界值,就没有奇数了,因为按照IEEE 754标准的舍入规则,都是舍入到偶数的。
console.log(0.5+0.1==0.6);  //true
console.log(0.1+0.2==0.3);  //false  
//因为二进制浮点数无法精确表示大部分十进制小数,所以有了很多常见的怪异,比如: 0.1 + 0.2 打印结果是0.30000000000000004
console.log(Math.max());    //-Infinity
console.log(Math.min());    //Infinity
console.log([]+[]);     //""
console.log([]+{});     //[object Object]  //空对象转换成字符串在拼接
console.log({}+[]);     //[object Object]
console.log(true+true+true===3);    //true
console.log(true-true);     //0
console.log(true==1);       //true
console.log(true===1);      //false
console.log((!+[]+[]+![]).length);      //9
//假假得正,字符串拼接上,假正得假,所以得到"truefalse"
console.log(9+"1");     //91
console.log(91-"1");    //90
console.log([]==0);     //true

7、对象比较的是引用

var obj1 = {age:23}
var obj2 = {age:23}
console.log(obj1 === obj2); //false
console.log(obj1 == obj2);  //false
var obj3 = obj2;
console.log(obj3 === obj2); //true
console.log(obj3 === obj1); //false

8、arguments 是个类数组对象,能使用length,但不能使用数组的方法, 如何使用数组方法呢?

function test(){
        console.log(arguments.length); //6
        var args = [].slice.call(arguments) 
        //你也可以这样
//      var args1 = Array.prototype.slice.call(arguments)
        return args.reverse() //[6, 5, 4, 3, 2, 1]
      }
      console.log(test(1,2,3,4,5,6));
      console.log(test.length);  //获取函数行参的个数

  9、对象判断

var obj = {}
var arr = []
//不ok的
console.log(typeof obj); //object
console.log(typeof arr); //object
//不ok的
console.log(obj instanceof Object) //true
console.log(arr instanceof Array)  //true
console.log(arr instanceof Object) //true
//ok的
var resOjb = Object.prototype.toString.call(obj);
var resArr = Object.prototype.toString.call(arr);
console.log(resOjb);  //[object Object]
console.log(resArr);  //[object Array]

10、console.dir()可以显示一个对象所有的属性和方法

11、[]==![]

因为==的存在,右边的数组[]会先调用数组的valueOf方法,因为[]为空,所以返回的是本身(如果不为空,则返回数组的内容),即返回数组对象,对象转换为布尔值为 true ,取反后为 false ,左边为空数组,直接转换为布尔值为 false

12、! 操作符会将操作数的值转为布尔类型,然后取反。连续使用两次等价于将操作数转为布尔类型。相当于其他语言中的强制类型转换

13、当需要在src请求添加查询字符串以改变src路径时(使用点击更改验证码时能用上),不需要使用new Date(). getTime()来获取时间戳,也不需要使用Math.random() ,在方法自带的event事件中,可以通过e.timeStamp (返回浏览器打开到触发事件的毫秒数)获取时间戳

14、使用js判断客户端,如果是手机端就使用手机端页面,否则使用pc页面

function browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
        window.location.href="http://blog.csdn.net/lmj623565791";
    } else {
        window.location.href="http://m.blog.csdn.net/blog/index?username=lmj623565791";
    }
}
browserRedirect();

15、判断字符串是不是中文汉字

if (escape(value).indexOf("%u") != -1) { }//汉字

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值