0

我在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 函数的文字代码。此功能是否不再适用于现代浏览器?

4

3 回答 3

1

似乎在铬 v25 上工作得很好

Javascript

function makePerson(first, last) {
    return {
        first: first,
        last: last,
        fullName: function() {
            return this.first + ' ' + this.last;
        },
        fullNameReversed: function() {
            return this.last + ', ' + this.first;
        }
    }
}

var s = makePerson("Simon", "Willison");
console.log(s.fullName());
console.log(s.fullNameReversed());

输出

Simon Willison
Willison, Simon 

jsfiddle 上

于 2013-05-07T23:01:33.107 回答
1

如果您收到该函数的代码,可能是因为您调用了该函数s.fullName而不是s.fullName()(您缺少括号)

于 2013-05-07T23:06:19.147 回答
1

听起来您在函数调用结束时错过了括号。

尝试

s.fullName();

代替

s.fullName;
于 2013-05-07T23:10:17.613 回答