在不同的.js文件中使用两个同名的方法

时间:2010-08-16 11:21:36

标签: javascript

我有两个同名的方法,用于两个不同的.js文件中的不同目的。如何在同一页面上使用这些方法?

在Count.js中:

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

在PriceImplement.js

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

2 个答案:

答案 0 :(得分:22)

你应该养成命名JavaScript文件的习惯:

// 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]
};

然后调用Namespace.method之类的方法,即Price.add()

答案 1 :(得分:8)

如果它们都是使用函数声明定义的,比如

function iHaveTheSameNameAsAnotherFunction(params) {
    …
}
那么你不能。第二个声明只会覆盖第一个声明。

相关问题