// 一个接一个运行
// timeout 不能直接放在 for 里面,暂时不知道为什么
function functionOneByOne(fn, times, duration) {
  for(var i=0; i<times; i++) {
    timecout(i);
  }
  function timecout(index) {
    setTimeout(function(){
      if (fn) fn(index);
    }, duration*index);
  }
}
// 区间内持续时间的变化,比如可以做平滑动画什么的
function smooth(start, end, duration, fn) {
  var start = start || 0,
    end = end || 0,
    offset = end - start,
    duration = duration || 1000,
    now = Date.now(); a();   function a() {
    var x = Math.min(1, (Date.now() - now) / duration);
    if (fn) fn(offset * x + start);
    1 > x && setTimeout(a, 10);
  }
}

  

// 求数组 arr 中的最大最小值
Math.max.apply(Math, arr);
Math.min.apply(Math, arr);

  

//字符串去首尾空格
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, "");};

  

// 区间内的随机数
function RandomNumber(min, max) {
  return (min||0) + Math.random() * ((max||1) - (min||0));
}

  

// 获取 search 中对应键的值
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

  

// 生成一个连续数字的数组
function creatNumberArray(length, start) {
  var arr = []; start = start || 0;
  for(var i=start; arr.push(i++)<length;);
  return arr;
}

  

// 数字数组 arr 改成从小到大排序
arr.sort(function(a,b){return Math.random()>0.5 ? -1 : 1;});

  

// 获得 dom 元素,可传入 类名/索引/对象
function getElem(o, box) {
if (typeof o == "string") return document.querySelector(obj);
else if (typeof o == "number") return getObj(box).eq(o)[0];
else if (typeof o == "object") {
if (typeof o.css == "function") return o[0];
else return o;
}
}
// 获取 jquery 元素,参数同上
function getObj(o, box) {
if (typeof o == "string") return $(o);
else if (typeof o == "number") return getObj(box).eq(o);
else if (typeof o == "object") {
if (typeof o.css == "function") return o;
else return $(o);
}
}

  

// 将 html 编译成 text
function encodeHTML( str ) {
  var elem = document.createElement('span');
  elem.appendChild( document.createTextNode( str ) );
  return elem.innerHTML;
}
// 将 text 编译成 html
function decodeHTML( str ) {
  str = str.replace(/</g,'<').replace(/>/g,'>')
  var elem = document.createElement('span');
  elem.innerHTML = str;
  return elem.textContent || elem.innerText;
}

  

// 产生随机颜色
function randomColor() {
  var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16);
  if (rand.length == 6) return '#' + rand;
  else return randomColor();
}

  

这些方法个人都用得挺多的,然后再分享一个最近发现的装逼技能。

var x = i * i, i == 5 && fn();
// “,” 虽然看上去和 “;” 类似,但更容易表明 i 和 fn() 有基情
// 其次,&& 则类同于 if(i == 5) fn(); 是不是很带感, i < 10 || fn(); 则能等同于 if(i != 5) fn();

一些逼格略高的 js 片段的更多相关文章

  1. 编写高质量JS代码的68个有效方法(八)

    [20141227]编写高质量JS代码的68个有效方法(八) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  2. 编写高质量JS代码的68个有效方法(七)

    [20141220]编写高质量JS代码的68个有效方法(七) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  3. 编写高质量JS代码的68个有效方法(六)

    [20141213]编写高质量JS代码的68个有效方法(六) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  4. 编写高质量JS代码的68个有效方法(四)

    [20141129]编写高质量JS代码的68个有效方法(四) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  5. 编写高质量JS代码的68个有效方法(三)

    [20141030]编写高质量JS代码的68个有效方法(三) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  6. 编写高质量JS代码的68个有效方法(二)

    [20141011]编写高质量JS代码的68个有效方法(二) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  7. JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)

    编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...

  8. 广告等第三方应用嵌入到web页面方案 之 使用js片段

    在自己的项目中嵌入过广告的朋友们可能都用过百度联盟, 只需要嵌入如下一段js代码片段, 就可以在自己的项目中嵌入广告, 来获得收益. <script type="text javasc ...

  9. 编写高质量JS代码中

    前段时间看了几道关于前端javascript的面试题目,方觉函数调用模式等基础的重要性.于是,下定决心,好好补补基础,即便不能深入语言的内部设计模式,也要对基本面向对象概念有比较深入的理解. 继续上一 ...

随机推荐

  1. springMVC对简单对象、Set、List、Map的数据绑定和常见问题.

    算了,就不粘贴了,到原文去查看吧! springMVC对简单对象.Set.List.Map的数据绑定和常见问题.

  2. mybatis执行多条sql语句

    1,mybatis执行多条sql语句,有以下几种思路, a,存储过程 b,修改jdbc的参数,允许执行多条语句,如下所示: sqlserver可以直接使用begin,end来执行多条语句, mysql ...

  3. 回调函数(callback)是什么?

    你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货.在这个例子里,你的电话号码就叫回调函数,你把电话留给 ...

  4. 怎么在Word中找MathType菜单

    一些用户朋友在使用word的过程中,发现自己突然找不到MathType公式编辑器菜单项了,而这个时候又急着编写公式,所以会特别的着急.下面我们就来针对这个问题好好的给大家分析一下,并提供解决方案.请关 ...

  5. PHP和Java 加解密

    http://www.jb51.net/article/64961.htm http://www.jb51.net/article/129218.htm http://www.jb51.net/art ...

  6. 编译Hadoop1.1.2eclipse插件并测试

    (一).building hadoop 1.编辑{HADOOP_HOME}/build.xml (1).对31行的hadoop版本做修改 <property name="version ...

  7. 七牛上传ipa后自动生成plist文件

    1.利用模板技术动态生成plist文件的内容:(模板内容和data替换为plist需要的内容) //artTemplate <script src="js/template.js&qu ...

  8. poj1840

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 15133   Accepted: 7426 Description ...

  9. 《从零开始学Swift》学习笔记(Day 44)——重写属性

    原创文章,欢迎转载.转载请注明:关东升的博客 重写实例属性 我们可以在子类中重写从父类继承来的属性,属性有实例属性和静态属性之分,他们在具体实现也是不同的. 实例属性的重写一方面可以重写getter和 ...

  10. angularjs 复选框 单选框

    关于复选框,在做项目的时候,有一下几点心得 单选框 1.判断哪个单选框选中的情况 html代码 判断该复选框是否选中 $scope.agree.isChecked     判断这个值,如果等于1,代表 ...