我正在查看Bootstrap 4的源代码,并且发现他们使用了es6类以及某种显示模块模式。为什么Bootstrap 4在es6类中使用私有方法?
这是一个取自here的代码的简单示例。
const Modal = (($) => {
const NAME = 'modal'
const VERSION = '4.0.0-alpha.3'
...
const Default = {
...
}
class Modal {
constructor(element, config) {
this._config = this._getConfig(config)
this._element = element
...
}
// public
toggle(relatedTarget) {
...
}
show(relatedTarget) {
...
}
hide(event) {
...
}
dispose() {
...
}
// private
_getConfig(config) {
...
}
_showElement(relatedTarget) {
...
}
_enforceFocus() {
...
}
_setEscapeEvent() {
...
}
_setResizeEvent() {
...
}
}
return Modal
})(jQuery)
export default Modal
这会导致每个方法或属性被暴露,包括私人的。但是,这在最终产品中不会发生。例如,像$('#myModal').modal('_getConfig')
这样的东西不起作用。发生什么事?
大概'modal'确实检查名字是否以下划线开头...... – Bergi