Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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:

function add()
{
// some manipulation doing here
}

In PriceImplement.js

Function add()
{
// some manipulation doing here
}
like image 216
Pankaj Avatar asked Aug 16 '10 11:08

Pankaj


2 Answers

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()

like image 187
PatrikAkerstrand Avatar answered Oct 14 '22 00:10

PatrikAkerstrand


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.

like image 25
Marcel Korpel Avatar answered Oct 13 '22 23:10

Marcel Korpel