Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CoffeeScript:如何声明可以从Array.reduce中使用的私有函数?_Javascript_Arrays_Coffeescript_Scope_Hoisting - Fatal编程技术网

Javascript CoffeeScript:如何声明可以从Array.reduce中使用的私有函数?

Javascript CoffeeScript:如何声明可以从Array.reduce中使用的私有函数?,javascript,arrays,coffeescript,scope,hoisting,Javascript,Arrays,Coffeescript,Scope,Hoisting,主要目标是按任何给定属性从数组中筛选重复项。我尝试使用的解决方案是js@ 我试图在coffeescript中实现它。除了函数的范围之外,一切都很好。我不希望从外部调用\u indexOfProperty函数,因为它在所有其他上下文中都是无用的。但如果我将其私有化(通过在声明中删除@),我就不能从inputArray.reduce中调用它 我的咖啡代码如下所示: Utils = -> @filterItemsByProperty= (inputArray,property)=>

主要目标是按任何给定属性从数组中筛选重复项。我尝试使用的解决方案是js@

我试图在coffeescript中实现它。除了函数的范围之外,一切都很好。我不希望从外部调用\u indexOfProperty函数,因为它在所有其他上下文中都是无用的。但如果我将其私有化(通过在声明中删除@),我就不能从inputArray.reduce中调用它

我的咖啡代码如下所示:

Utils = ->
    @filterItemsByProperty= (inputArray,property)=>
        if not _.isArray inputArray
            return inputArray
        r = inputArray.reduce(((a,b,c,d,e)=>
                if @._indexOfProperty(a,b,property) < 0
                    a.push(b)
                a
            ),[])
        r

    @_indexOfProperty= (a,b,prop) ->
        i = 0
        while i< a.length
            if a[i][prop] == b[prop]
                return i
            i++
        -1

    return

window.utils = Utils
现在,任何人都可以做到这一点:

App.utils._indexOfProperty(1,2,3)

如何修改咖啡以停止此操作?

能否删除“@”,这次在filterItemsByProperty范围内定义一个局部变量“indexOfProperty”,并将其指定为“\u indexOfProperty”(这样可以在reduce()中使用“indexOfProperty”)

@filterItemsByProperty=(输入阵列,属性)->
indexOfProperty=\u indexOfProperty
如果。iArray(输入阵列)
返回输入阵列
r=输入阵列。减少((a,b,c,d,e)->
如果索引属性(a、b、属性)<0
a、 推b
A.
), [])
R

不要将
\u indexOfProperty
放在
上,它将不可见:

Utils = ->
    _indexOfProperty = (a,b,prop) ->
        i = 0
        while i< a.length
            if a[i][prop] == b[prop]
                return i
            i++
        -1

    @filterItemsByProperty= (inputArray,property)=>
        if not _.isArray inputArray
            return inputArray
        r = inputArray.reduce(((a,b,c,d,e)=>
                if _indexOfProperty(a,b,property) < 0
                    a.push(b)
                a
            ),[])
        r

    return

window.utils = Utils
Utils=->
_indexOfProperty=(a、b、prop)->
i=0
而我
如果不是uu.isArray输入阵列
返回输入阵列
r=输入阵列。减少((a,b,c,d,e)=>
如果_indexOfProperty(a,b,property)<0
a、 推动(b)
A.
),[])
R
返回
window.utils=utils

谢谢,它成功了,我很惊讶。正如问题中已经提到的,我知道删除@会使函数私有化。但我不明白为什么我不能从内部访问它。今天早些时候减少?明天我得查一查。对不起,我一定读过了。可能您的reducer函数中仍然有
@
符号,或者您可能有输入错误:)
@filterItemsByProperty = (inputArray, property) ->
    indexOfProperty = _indexOfProperty
    if !_.isArray(inputArray)
      return inputArray
    r = inputArray.reduce(((a, b, c, d, e) ->
      if indexOfProperty(a, b, property) < 0
        a.push b
        a
      ), [])
    r
Utils = ->
    _indexOfProperty = (a,b,prop) ->
        i = 0
        while i< a.length
            if a[i][prop] == b[prop]
                return i
            i++
        -1

    @filterItemsByProperty= (inputArray,property)=>
        if not _.isArray inputArray
            return inputArray
        r = inputArray.reduce(((a,b,c,d,e)=>
                if _indexOfProperty(a,b,property) < 0
                    a.push(b)
                a
            ),[])
        r

    return

window.utils = Utils