0

I am trying to understand Javascript concepts from https://developer.mozilla.org/en-US/docs/JavaScript/A_re-introduction_to_JavaScript . Please see the code below;

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;
}

I am confused why function personFullName() is called like

this.fullName = personFullName;

why it is not called like;

this.fullName = personFullName();

And same for the below;

this.fullNameReversed = personFullNameReversed;

I know functions are objects in javascript but i am unable to understand this concept?

1 Answer 1

1

Because the Person object is assigning itself a method, not the results of a function. That is the reason it doesn't call the function.

This way you can do this.

var p = new Person("Matt", "M");
p.fullName(); // Returns "Matt M"
p.fullNameReversed(); // Returns "M, Matt"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.