Reputation: 23
I want to check my idea is correct or not?
Codes:
function Person(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
Person.prototype.getFullName = function () {
return this.firstname + ' ' + this.lastname;
}
var john = new Person('Melo','Tang');
I call the code in the following picture "function constructor".
function Person(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
When the program run to this line
var john = new Person('Melo','Tang');
JS will use prototype chain to add the getFullName function in the "function constructor" Object and new a empty Object like following picture am I right?
Upvotes: 0
Views: 73
Reputation:
I don't really understand what your diagram or the arrows or their colors are supposed to represent.
When the program runs to this line
var john = new Person('Melo','Tang');
What happens here is exactly that a new object is constructed via the Person
constructor, with Person.prototype
as its prototype. Other than being used as the prototype for the new object, Person.prototype
is not examined or consulted, nor is anything done with the methods on it at this time. The prototype is consulted only when looking up properties and methods, such as when calling john.getFullName()
.
For clarity, I would avoid the term "function constructor object". Just call it a "constructor (function)".
Upvotes: 2