我们刚刚讨论了事件名称是使用现在时形式还是过去时形式。事实上,事件通常在发生某事后触发:
store.save(object)
store.trigger("create", object)
我想知道事件命名是否有任何约定?使用过去形式会更有意义,另一方面我觉得使用现在形式使用得更广泛。
关于这个主题有什么好的资源吗?您是否知道已知的 JavaScript 库使用过去形式来表示其事件?
最佳答案
我会用现在时。
大多数(如果不是全部)JavaScript 框架似乎都遵循 JavaScript DOM 事件 API 规定的约定;即使用现在时作为事件名称。当我想到这一点时,这对我来说似乎是最自然的,尽管事件是在触发后进行处理的。毕竟,事件是在事件名称上触发的。无论执行什么操作都会触发事件,它都会发生在事件循环的当前迭代中。换句话说:就事件循环而言,事件和触发事件的 Action 同时发生。
一些框架,例如YUI3 ,为自定义事件提供 after
Hook 和 on
Hook 。这种区别在 YUI 的属性库(以及其他属性库)中得到了应用:
Listeners registered using the
on
method, are notified before the stored state of the attribute has been updated. [...]Since these listeners are invoked before any state change has occurred, they have the ability to prevent the change in state from occurring [...]
Listeners registered using the
after
method, are notified after the stored state of the attribute has been updated.
来源:http://yuilibrary.com/yui/docs/attribute/index.html#on-vs-after
我个人并不知道有任何框架使用过去时作为事件名称,但我也不熟悉所有这些框架。
关于JavaScript 事件命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450397/