避免Coffeescript中命名冲突的最佳实践 问题:

避免Coffeescript中命名冲突的最佳实践 问题:,coffeescript,naming-conventions,Coffeescript,Naming Conventions,在Coffeescript中避免命名冲突的最佳实践是什么,特别是在嵌套作用域和Ajax回调的上下文中 我的问题 我遇到了一个名称冲突问题,我的命名约定规定Ajax回调中的数据名称与我的作用域中的另一个对象相同 在下面的代码中,我将所有函数都放在对象notifications上,但是来自Ajax GET请求的数据被命名为notifications。结果显然导致了错误: # Handles initial get request. notifications.init = -> $.ajax

在Coffeescript中避免命名冲突的最佳实践是什么,特别是在嵌套作用域和Ajax回调的上下文中

我的问题 我遇到了一个名称冲突问题,我的命名约定规定Ajax回调中的数据名称与我的作用域中的另一个对象相同

在下面的代码中,我将所有函数都放在对象
notifications
上,但是来自Ajax GET请求的数据被命名为
notifications
。结果显然导致了错误:

# Handles initial get request.
notifications.init = ->
$.ajax 'notifications',
  type: 'GET'
  dataType: 'json'
  error: (jqXHR, textStatus, errorThrown) ->
    alert textStatus
  success: (notifications, textStatus, jqXHR) ->
    if notifications?
      filteredNotifications = notifications.filteredNotifications notifications
      notifications.behavior notifications

# Triggers the notifications
notifications.behavior = (filteredNotifications) ->
  if filteredNotifications?
    $('#counter').html filteredNotifications.length
  if parseInt($('#counter').html()) > 0
    $('#counter').css
      'background': 'black'

# Removes notifications sent by the current user, copies for the other user,
# and notifications marked as observed.
notifications.filteredNotifications = (notifications) ->
  filteredNotifications = filteredNotifications.filter((notification) ->
    notification.recipients.username is $username() and
    notification.copy_for_user_id is $id() and
    notification.observed is false
  )
  return filteredNotifications
考虑
我已经考虑过Ajax回调中数据对象的
通知的各种缩写,但是它降低了可读性。以不同的方式命名父对象似乎也不合适。

以下是我的建议。正如你所说,这个主题非常主观,但我一直努力保持这个标准

两个要点:

  • 将逻辑从名称空间中抽象出来是一种很好的做法,这样可以减少与之冲突的符号。Coffeescript允许您实例化匿名类,这在这方面很有帮助
  • 我专门使用标识符
    data
    作为JSON回调中数据对象的名称。这减少了任何阅读回调的人的困惑,并有助于消除像您遇到的那样的冲突

    notifications = new class
        init: -> $.ajax 'notifications',
            type: 'GET'
            dataType: 'json'
            error: (jqXHR, textStatus, errorThrown) ->
                alert textStatus
            success: (data, textStatus, jqXHR) =>
                if data?
                    @behavior @filter data
    
        # Triggers the notifications
        behavior: (notifications) ->
            if notifications?
                $('#counter').html notifications.length
                if notifications.length?
                    $('#counter').css
                        'background': 'black'
                return
    
        # Removes notifications sent by the current user, copies for the other user,
        # and notifications marked as observed.
        filter: (notifications) ->
            return notifications.filter (notification) ->
                notification.recipients.username is $username() and
                notification.copy_for_user_id is $id() and
                notification.observed is false
    
    notifications.init()
    
我还做了一些其他的小改动,我冒昧地在
行为
中做了一个逻辑改动,这可能不适合您的情况,但无论如何您都应该修改那里的逻辑。使用DOM在应用程序逻辑中存储所需的值是不明智的


需要注意的重要一点是在
success
回调中使用了“胖箭头”(
=>
)。它是必需的,这样您就可以将函数绑定到正确的上下文,并正确解析
@behavior
@filter
了。

我可以得到否决的原因吗?关于这个问题的那个呢?在我看来,这个问题很好,但以意见为基础,在这种情况下,这个答案是令人满意的。标记这个问题似乎完全合理,但否决它和我的答案。。。不。显然,我是有偏见的,但我不认为我有那么远的根据。这不是基于意见。这里要问的真正问题是“如何在编程中避免名称冲突,重点是嵌套的Coffeescript作用域?”。如果您要结束此操作,您可能还应该删除下的大多数问题。人们提出了大量质量更为可疑的问题。肯定是的,而且得了9分。