Dilu
Dilu

Reputation: 13

How a method or function do same thing when we create two difference object/instance on Javascript?

When I read this Prototypes in JavaScript article on internet,I saw this code

function Human(firstName, lastName) {
    this.firstName = firstName,
    this.lastName = lastName,
    this.fullName = function() {
        return this.firstName + " " + this.lastName;
    }
}

var person1 = new Human("Virat", "Kohli");

console.log(person1);

when he explains about this, he mentions "two instances of function fullName that do the same thing.". I confuse at this point. How this function doing same thing? because this function will be show two difference full name therefore how is it possible to do same thing? any one can help me?

Upvotes: 0

Views: 51

Answers (1)

user12077300
user12077300

Reputation:

It means the output of fullName will always be firstName + lastName of new object ... So fullName always use same properties of new objects.

Upvotes: 1

Related Questions