【发布时间】:2014-05-02 09:36:33
【问题描述】:
在 javaScript 中,我创建如下所示的对象。当我将对象解析为函数时,我通常在对象之后命名参数。 [见下面的例子]。我的理由是,如果另一个开发人员出现并查看一个他们立即知道该函数需要 fooObj、barObj 和 laaObj 的函数。问题是:是否有任何可能与此相关的语法/性能问题。 (例如,某些浏览器会遇到参数名称与对象/“函数”同名的问题)。
function exampleObj(name, date, wine, cheese) {
this.name = name;
this.date = new Date(date);
this.wine = wine;
this.cheese = cheese;
this.drink = function() {
open(this.wine);
pour(this.wine);
consume(this.wine);
}
this.eat = function() {
unwrap(this.cheese);
getCrackers();
informGromitOfCheese(this.cheese);
consume(this.cheese);
}
}
var example1 = new exampleObj("Foo", "02/05/2014", "Merlot", "Stinky Bishop");
当我将对象传递给函数时:
// Pass the exampleObj here. Param name == Function name
function foo(exampleObj) { // <-- Is this okay?
alert(exampleObj.name);
}
foo(example1);
【问题讨论】:
-
@PaulS。我想他想知道参数名称是否可以与任何其他函数相同。他实际上是通过
example1 -
@thefourtheye 是的,我的“对象”基本上是我存储的函数。
-
@User2 你的用户名真的很酷,顺便说一句。
-
您可以按照您的说明进行操作,但是您正在遮蔽您的
exampleObj构造函数,这意味着您可能没有可用的引用,因此无法构造或检查instanceof在foo里面很容易。 -
@Edward Flattery -> 获得 +1 的最佳方式。
标签: javascript function object parameter-passing