//类对象转成数组

var domNodes = Array.prototype.slice.call(document.getElementsByTagName("*"));   var domNodes = Array.prototype.slice.call(arguments);

向下取整:

var a = ~~1.2; //1

这个补充一下,还可以用位右移符>>

var a = 3.4>>0; //3

但是两者最好都只用在正整数上,因为只是舍掉了小数部分。Math.floor(-1.2)应该为-2,这两种方法的结果为-1

转数字

var time = + new Date();

设默认值

function foo(bar){
var foobar = bar || 'default';
//bar 为 undefined, null, "", 0, false, NaN 时最后都得到'default'
} //坑
[]||"aa"; //[]
{}||"aa"; //SyntaxError
({})||"aa";//Object {}

NaN的坑

//NaN - 不是一个数字
isNaN(a);
//检查是不是 "不是一个数字" .. isNaN(null);//false
//实际上是null被转为0了,表面上"null 不是一个 不是数字 的东西"。wtf...

UNICODE 用作变量名

var \u4f60\u597d = "\u4f60\u597d";
var b = {};
b.\u4f60\u597d = \u4f60\u597d;
console.log(b);//Object {你好: "你好"}
console.log(b.你好);// "你好"
console.log(b.\u4f60\u597d);// "你好" console.log("你"==="\u4f60");//true

数组传递和复制

var a = [1,2,3];
var b = a;
delete b[1];
console.log(a);//[1, undefined × 1, 3] var a = [4,5,6];
var b = a.slice(0);
delete b[1];
console.log(a);//[4,5,6]
console.log(b);//[4, undefined × 1,6]

对象与Function

console.log(typeof Function);//"function"
console.log(typeof Object);//"function"

函数声明

aa();
function aa(){return true;} //true bb();
var bb = function(){return true;} //TypeError

toString()

2.toString();//SyntaxError
2 .toString(); //"2"
2..toString(); //"2"
(2).toString(); //"2" [1,[2,"abc","",0,null,undefined,false,NaN],3].toString();
//"1,2,abc,,0,,,false,NaN,3"

for in 暴露原型链属性

Object.prototype.foo = 1;

var obj = new Object();
obj.bar = 1;
for(var p in obj){
console.log(p);//bar,foo 都遍历出来了。可以用hasOwnProperty()过滤原型链属性 }

switch代替if else

 switch (true) {
case (a > 10):
do_something();
break;
case (a < 100):
others();
break;
default:
;
break;
};
 

漂亮的随机码

Math.random().toString(16).substring(2); //14位
Math.random().toString(36).substring(2); //11位
合并数组:
var a = [1,2,3];
var b = [4,5,6];
Array.prototype.push.apply(a, b);
console.log(a); //[1,2,3,4,5,6]
用0补全位数:
function prefixInteger(num, length) {
return (num / Math.pow(10, length)).toFixed(length).substr(2);
}

交换值:
a= [b, b=a][0];
将一个数组插入另一个数组的指定位置:

var a = [1,2,3,7,8,9];
var b = [4,5,6];
var insertIndex = 3;
a.splice.apply(a, Array.concat(insertIndex, 0, b));
// a: 1,2,3,4,5,6,7,8,9
快速取数组最大和最小值

Math.max.apply(Math, [1,2,3]); //3
Math.min.apply(Math, [1,2,3]); //1

条件判断:

var a = b && 1;
//相当于:
if (b) {
a = 1;
}else {
a = b;
} var a = b || 1;
//相当于
if (b) {
a = b;
} else {
a = 1;
}

判断数组:

Object.prototype.toString.call([]) === '[object Array]';

判断IE:

var ie = /*@cc_on !@*/false;

取整:
a = 5.2|0

string转数字
a = +'123'

4. 去除数组中重复的值:

function removeRepeat(arr){
return arr.filter(function(elem, pos) {
return arr.indexOf(elem) == pos;
});
} var arr = new Array("1","2","3","4","4","4","4","5");
var newArr = removeRepeat(arr);
console.log(newArr); //1,2,3,4,5
function sum(arr){ return arr.reduce(function(s,n){ return s+n }); } var arr = new Array(1,2,3,4); console.log(sum(arr)); // 10
6. 寻找数组中的最大数

var  numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var maxNumber = Math.max.apply(null, numbers);
console.log(maxNumber);

判断是否为数组

 function isArray(arr){ return arr ? Object.prototype.toString.call(arr) === '[object Array]' : false }

转数字

var time = + new Date();

  • false => ![]
  • true => !![]
  • undefined => [][[]]
  • NaN => +[![]]
  • 0 => +[]
  • 1 => +!+[]
  • 2 => !+[]+!+[]
  • 10 => [+!+[]]+[+[]]
  • Array => []
  • Number => +[]
  • String => []+[]
  • Boolean => ![]
  • Function => []["filter"]
  • eval => []["filter"]["constructor"]( CODE )()
  • window => []["filter"]["constructor"]("return this")()

http://www.jsfuck.com/

 

js的常用小技巧的更多相关文章

  1. Matlab常用小技巧及部分快捷键

    Matlab常用小技巧一: 1. m文件如果是函数,保存的文件名最好与函数名一致,这点都很清楚.不过容易疏忽的是,m文件名的命名尽量不要是简单的英文单词,最好是由大小写英文/数字/下划线等组成.原因是 ...

  2. js 数组去重小技巧

    js 数组去重小技巧 Intro 今天遇到一个问题,需要对数据进行去重,想看一下有没有什么比较方便的方法,果然有些收获. Question 问题描述: 我有一个这样的数据: [ { "Pro ...

  3. javascript的40个网页常用小技巧

    下面是javascript的40个网页常用小技巧,对网站开发人员相信会有帮助.1. oncontextmenu="window.event.returnValue=false" 将 ...

  4. js几个小技巧和坑

    蝴蝶书看了,也知道充满了毒瘤和糟粕,但该用还是得用. 实际写了几天,小技巧记录下来.都是在py里有直接答案,不会遇到的问题,没想到js里这么费事. 还是要多读<ES6标准入门> 1判断ob ...

  5. HBase Shell Get 操作常用小技巧

    在工作中,有时候只是想简单看下HBase表某些关键指标的值,这个时候总不能现写Java代码去查看,以下几个小技巧你可能会经常用到. 1. 某行有许多列,只想获取指定2~3列的数据 hbase> ...

  6. 今天整理了几个在使用python进行数据分析的常用小技巧、命令。

    提高Python数据分析速度的八个小技巧 01 使用Pandas Profiling预览数据 这个神器我们在之前的文章中就详细讲过,使用Pandas Profiling可以在进行数据分析之前对数据进行 ...

  7. JS常用小技巧

    iframe子窗口调用父窗口方法 parent.functionName(); 二.iframe 父窗口和子窗口相互的调用方法 1.IE中使用方法: 父窗口调用子窗口:iframe_ID.iframe ...

  8. js开发中常用小技巧

    1.获取指定范围内的随机数 function getRadomNum(min,max){ return Math.floor(Math.random() * (max - min + 1)) + mi ...

  9. Scala的常用小技巧

    1."RichString.java".stripSuffix(".java") == "RichString" "http:// ...

随机推荐

  1. Apache Commons 工具集介绍

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.下面是我这几年做开发过程中自己用过的工具类做简单介绍. 组件 功能介绍 BeanUtils 提供了对于 ...

  2. 【全局变量】mysql查看全局变量以及设置全局变量的值

    1.查看mysql的所有全局变量的值 SHOW GLOBAL VARIABLES 或者 SHOW VARIABLES mysql有很多全局变量,包括系统的一些基本信息,以及mysql的一些基本配置都可 ...

  3. poj 1637 Sightseeing tour 混合图欧拉回路 最大流 建图

    题目链接 题意 给定一个混合图,里面既有有向边也有无向边.问该图中是否存在一条路径,经过每条边恰好一次. 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d ...

  4. springBoot api接口

    application/json 请求接口 @RequestMapping(value = "/getBaseData", method = RequestMethod.POST, ...

  5. Sprak RDD简单应用

    来自:http://my.oschina.net/scipio/blog/284957#OSC_h5_11 目录[-] 1.准备文件 2.加载文件 3.显示一行 4.函数运用 (1)map (2)co ...

  6. IntelliJ中的Scala入门

    IntelliJ IDE中的Scala入门 创建项目 打开IntelliJ并单击File => New => Project 在左侧面板中,选择Scala.在右侧面板中,选择IDEA. 将 ...

  7. FZU 1057 a^b 【数论/九余定理】

    Accept: 1164    Submit: 3722Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description 对于任 ...

  8. 洛谷——1115 最大子段和(区间DP)

    题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度. 第2行包含N个绝对值不大于10000 ...

  9. MySQL备份工具收集

    说明:MySQL的备份不像SQL Server那么的简单,备份时需要分数据库引擎类型,现在主流的就两个:InnoDB和MyISAM,而这两种类型备份方式各不一样. MyISAM: mysqlhotco ...

  10. 如何在window上把你的项目提交到github

    1.首先你需要在https://github.com/ 上注册一个账户 2.注册成功以后,你需要新建一个repository(储藏室),这个用来存放你要上传的项目 点击中间的带加号的图标就可以新建re ...