My understanding is that when you call(我的理解是,当您打电话时)
numbers.filter(range.isValid)
really what you are doing is passing the function by referrence or(实际上,您正在做的是通过引用传递函数或)
numbers.filter(isValid(number) {
return number >= this.minNumber && number < this.maxNumber;
})
effectively the context is not transferred(eg maxNumber and minNumber are not set anywhere.)(有效地不传输上下文(例如,未在任何地方设置maxNumber和minNumber。))
when you do(当你做)
numbers.filter(number => range.isValid(number))
it works because the => function does not establish a context so it gets the context from the definition of range in the current scope.(之所以起作用,是因为=>函数未建立上下文,因此它从当前范围中的range定义获取上下文。)
when you do(当你做)
numbers.filter(range.isValid, range)
You are making use of the optional parameter to .filter described below(From W3C docs here: https://www.w3schools.com/jsref/jsref_filter.asp )(您正在使用可选参数对.filter进行如下所述(来自W3C文档: https ://www.w3schools.com/jsref/jsref_filter.asp))
Optional.(可选的。)
A value to be passed to the function to be used as its "this" value.(要传递给函数的值用作其“ this”值。) If this parameter is empty, the value "undefined" will be passed as its "this" value(如果此参数为空,则将值“ undefined”作为其“ this”值传递)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…