【发布时间】:2017-06-19 19:14:31
【问题描述】:
给定
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);
【问题讨论】:
-
new function(o) {...}正在尝试将该函数用作构造函数。 -
你从哪里得到
new?
标签: javascript anonymous-function function-declaration function-expression