0

I came across this example on the MDN and it doesn't work as described:

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

The article was written in 2006, and in both IE10 and Chrome 26, it just displays the literal code for the fullName and fullNameReversed functions. Is this functionality no longer working for modern browsers?

2
  • 9
    That code does work. Maybe you forgot the calling parentheses when you tried it? Commented May 7, 2013 at 22:58
  • Yes, it appears I forgot the calling parentheses. Thanks everyone. Commented May 8, 2013 at 14:26

3 Answers 3

1

It sounds like you've missed out the parentheses from the end of your function call.

Try

s.fullName();

instead of

s.fullName;
Sign up to request clarification or add additional context in comments.

Comments

1

Appears to work just fine on chromium 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());

Output

Simon Willison
Willison, Simon 

On jsfiddle

Comments

1

If you receive the code for the function, it is probably because you call the function like s.fullName and not s.fullName() (You are missing the parenthesis)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.