我在 MDN 上发现了这个例子并且它不像描述的那样工作:
function makePerson(first, last) {
return {
first: first,
last: last,
fullName: function() {
return this.first + ' ' + this.last;
},
fullNameReversed: function() {
return this.last + ', ' + this.first;
}
}
}
Example Call:
> s = makePerson("Simon", "Willison")
> s.fullName()
Simon Willison
> s.fullNameReversed()
Willison, Simon
这篇文章写于 2006 年,在 IE10 和 Chrome 26 中,它只显示 fullName 和 fullNameReversed 函数的文字代码。此功能不再适用于现代浏览器吗?
最佳答案
听起来您错过了函数调用末尾的括号。
尝试
s.fullName();
而不是
s.fullName;
关于JavaScript 在函数中嵌入函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429904/