给定
var stuffs = [
{ id : 1, name : "orange"},
{ id : 2, name : "apple"},
{ id : 0, name:"grapes"}
];
var filterMethod1 = new function(o){return (o.id>=1);}; // this gives undefined error for o
function filterMethod2(o) {return (o.id>=1);};
为什么使用匿名函数不适用于数组 filter() 方法?
var temp = stuffs.filter(new function(o){ return (o.id>=1);}); // o is undefined if used this way
使用声明的函数工作正常:
var temp = stuffs.filter(filterMethod2);