简体   繁体   English

为什么Bootstrap 4在es6类中使用私有方法?

[英]Why does Bootstrap 4 use private methods in es6 classes?

I was looking inside the source code of Bootstrap 4, and I discovered they're using es6 classes coupled with some revealing module pattern of some sort. 我在查看Bootstrap 4的源代码时,发现它们正在使用es6类以及某种形式的显示模块模式。

Here is a simplified example of code taken from 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

This would result in every method or property being exposed, including the private ones. 这将导致公开每个方法或属性,包括私有方法或属性。 However, this does not happen in the final product. 但是,这不会在最终产品中发生。 For example, something like $('#myModal').modal('_getConfig') would not work. 例如,像$('#myModal').modal('_getConfig')将无法工作。 What is happening? 怎么了?

It's only adding one function to the jQuery prototype _jQueryInterface : 它仅向jQuery原型_jQueryInterface添加一个函数:

  $.fn[NAME]             = Modal._jQueryInterface
  $.fn[NAME].Constructor = Modal
  $.fn[NAME].noConflict  = function () {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Modal._jQueryInterface
  }

  return Modal

})(jQuery)

If you look at the code of _jQueryInterface you'll see: 如果查看_jQueryInterface的代码,您将看到:

static _jQueryInterface(config, relatedTarget) {
  return this.each(function () {
    let data    = $(this).data(DATA_KEY)
    let _config = $.extend(
      {},
      Modal.Default,
      $(this).data(),
      typeof config === 'object' && config
    )

    if (!data) {
      data = new Modal(this, _config)
      $(this).data(DATA_KEY, data)
    }

    if (typeof config === 'string') {
      if (data[config] === undefined) {
        throw new Error(`No method named "${config}"`)
      }
      data[config](relatedTarget)
    } else if (_config.show) {
      data.show(relatedTarget)
    }
  })
}

If we look closely, you'll see that the instance of the class Modal is being saved as data : 如果仔细观察,您会发现Modal类的实例被另存为data

    if (!data) {
      data = new Modal(this, _config)
      $(this).data(DATA_KEY, data)
    }

You can access it in the same fashion the script does it (but only after creating it for the first time): 您可以用脚本执行它的相同方式来访问它(但只有在第一次创建它之后):

let data    = $(this).data(DATA_KEY)

DATA_KEY is bs.modal DATA_KEYbs.modal

Edit: 编辑:

$('#myModal').modal('_getConfig');

The function _getConfig is actually being called, it's just that function is returning the jQuery object not whatever the result of _getConfig is. _getConfig函数_getConfig是在被调用,只是该函数返回jQuery对象,而不管_getConfig的结果_getConfig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM