Skip to content

Instantly share code, notes, and snippets.

@zhuping
Last active March 24, 2016 07:10
Show Gist options
  • Save zhuping/93b21ead38d29da28c34 to your computer and use it in GitHub Desktop.
Save zhuping/93b21ead38d29da28c34 to your computer and use it in GitHub Desktop.
Javascript 奇技淫巧
//indexOf实现
// http://webreflection.blogspot.fr/2011/06/partial-polyfills.html
function indexOf(value) {
  for (var i = this.length; i-- && this[i] !== value;) {}
  return i;
}
var arr = [0, 1, 2, 3, 4];
indexOf.call(arr, 4);
// each的实现
function each(ary, func) {
    if (ary) {
        var i;
        for (i = 0; i < ary.length; i += 1) {
            if (ary[i] && func(ary[i], i, ary)) {
                break;
            }
        }
    }
}
var arr = [1,2,3,4]
each(arr, function(value, index) {console.log(value)})
@zhuping
Copy link
Author

zhuping commented Mar 24, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment