Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Last active August 29, 2015 14:02
Show Gist options
  • Save jikeytang/5c6948b0ab47b5a14101 to your computer and use it in GitHub Desktop.
Save jikeytang/5c6948b0ab47b5a14101 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140531-题目1
请实现javascript 1.8的filter方法
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
3. 今天只来一发。
@mailzwj
Copy link

mailzwj commented May 31, 2014

// 转自:http://www.zhangxinxu.com/wordpress/?p=3220#filter
if (typeof Array.prototype.filter != "function") {
    Array.prototype.filter = function(fn, context) {
        var arr = [];
        if (typeof fn === "function") {
            for (var k = 0, length = this.length; k < length; k++) {
                fn.call(context, this[k], k, this) && arr.push(this[k]);
            }
        }
        return arr;
    };
}

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