Use two methods of the same name in different .js files

后端 未结 2 1726
予麋鹿
予麋鹿 2020-12-09 05:47

I have two methods with the same name, for different purposes in 2 different .js files. How can I use those methods on same page?

In Count.js:

funct         


        
相关标签:
2条回答
  • 2020-12-09 05:59

    If they're both defined using function declarations, like

    function iHaveTheSameNameAsAnotherFunction(params) {
        …
    }
    

    then you can't. The second declaration will simply overwrite the first one.

    0 讨论(0)
  • 2020-12-09 06:07

    You should get into the habit of namespacing your JavaScript-files:

    //Count.js:

    var Count = {
      add: function add() {
      },
      [additional methods in the Count object]
    };
    

    // PriceImpl.js

    var Price = {
      add: function add () {
      },
      [additional methods for the Price implementation]
    };
    

    Then call methods like Namespace.method, i.e. Price.add()

    0 讨论(0)
提交回复
热议问题