0

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? enter image description here

2
  • The "Prototype Chain" label should be on the red arrow. Commented Dec 29, 2016 at 3:25
  • I forgot it thanks but except this there are any errors? Commented Dec 29, 2016 at 3:31

1 Answer 1

2

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)".

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

1 Comment

torazaburo thank you. As far as I know, Prototype is a property of an object and Proto is an Object. I call "function constructor object" because I want to emphasize function constructor is an Object in JS. I want to ask a question, the "Person.prototype" is a property of function constructor? If yes, getFullName function was added at Person.prototype or proto Obj?

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.