-1

Seems I didn't understand the constructor concept, So, I wrote some code to test it. Say you have the code like this:

var test=function(){...}

I know there is a property named constructor in the test.prototype object which point to the test object.

Here comes my question:

Is this property(constructor) only belongs to the prototype object ? or Do all the objects have the constructor property?

And I did another test. the code like below:

            function Shape() {
              this.x = 0;
              this.y = 0;
            }

            Shape.prototype.move = function(x, y) {
                this.x += x;
                this.y += y;
                console.info("Shape moved.");
            };

            Rectangle = Object.create(Shape);//inherit from the Shape instead of Shape.prototype
            Rectangle.constructor==Function//it is true.

I don't know where does Rectangle.constuctor come from or does it inherit from the Shape? thanks.

1 Answer 1

2

Object.create returns an object whose prototype is the object you pass it.

Therefore, since Shape.constructor is Function (Shape is a Function object), Rectangle inherits that.

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

4 Comments

Do all the objects have the constructor property?
Do you mean only Function object like Shape has the constructor? thanks.
I think it exactly comes from Function.prototype, Not as your said from the Shape ,because the document says All objects inherit a constructor property from their prototype, Am I right ?
@Joe.wang, You may have some confusion about prototypes. Read this article: javascriptweblog.wordpress.com/2010/06/07/…

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.