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);};
为什么使用匿名函数不是滤镜阵列()方法的工作?
var temp = stuffs.filter(new function(o){ return (o.id>=1);}); // o is undefined if used this way
使用声明功能正常工作:
var temp = stuffs.filter(filterMethod2);
'新功能(O){...}'试图使用该功能作为构造。 – Barmar
你从哪里得到那个'new'? – Bergi