将JS函数公开给全局命名空间

时间:2017-04-13 17:54:29

标签: javascript namespaces global

我正在尝试在全球范围内提供一项功能。我将它包装在一个立即函数中,但只有在函数内部调用$MyClass('myButton').click(function(){ console.log('The button was clicked'); }); 时它才有效。我想能够像Jquery一样使用它,在我的项目中的其他地方调用这样的函数:

   (function() {
      function $MyClass(element){
          element = document.getElementById(element)
          this.element = element;
          return this;
      }

      $MyClass.prototype.click = function(callback) {
          this.element.addEventListener("click", clickFunction);
          function clickFunction() {
              callback();
          }
      }

      $MyClass.prototype.hover = function(inEvent, outEvent) {
          this.element.addEventListener("mouseover", hoverOn);

          if (outEvent != undefined) {
              this.element.addEventListener("mouseout", hoverOff);
          }
          function hoverOn() {
              inEvent();
          }
          function hoverOff() {
              outEvent();
          }  
      }
}()); 

这是我想要访问的内容:

> git config remote.origin.url git@bitbucket.org:<my repo> # timeout=10
10:56:07 Fetching upstream changes from git@bitbucket.org:<my repo>
10:56:07  > git --version # timeout=10
10:56:08 using GIT_SSH to set credentials 
10:56:08 FATAL: ssh executable not found. The git plugin only supporofficial git client http://git-scm.com/download/win
10:56:08 java.lang.RuntimeException: ssh executable not found. The git plugin only supports official git client http://git-scm.com/download/win

如何将$ MyClass公开给全局命名空间?我在网上找到的答案都没有给我足够的意义,足以将它应用到我的项目中。

0 个答案:

没有答案