【发布时间】:2013-05-08 00:40:30
【问题描述】:
我在 MDN 网站上阅读 re-introduction to JavaScript 并在自定义对象部分看到了这个:
function personFullName() {
return this.first + ' ' + this.last;
}
function personFullNameReversed() {
return this.last + ', ' + this.first;
}
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = personFullName;
this.fullNameReversed = personFullNameReversed;
}
它在 MDN 网站上说,您可以在 Person 构造函数中引用 personFullName() 和 personFullNameReversed() 函数,只需键入它们的名称并将它们作为值分配给上面代码中所述的两个变量(this.fullName 和 this.fullNameReversed)。这对我来说都很清楚,但我的问题是为什么 personFullName 和 personFullNameReversed 旁边的括号被省略了?是不是应该说:
this.fullName = personFullName();
this.fullNameReversed = personFullNameReversed();?
从 MDN 网站的示例中呈现的方式,我觉得 Person 构造函数中的那些 fullName 和 fullNameReversed 属性指向一些已经声明的全局变量,而不是在 Person 构造函数之外声明的函数。
【问题讨论】:
标签: javascript function methods constructor custom-object