1

when the new object is constructed ,The object is set up to delegate any properties which haven't been explicitly set up to its constructor's prototype. That means that we can change the prototype later, and still see the changes in the instance.

first:

 function Foo(){}
 foo=new Foo();
 Foo.prototype={};
 foo.constructor==Foo//true.why is this happening since construtor prototype is empty object 

so the statement are not working as per the definition.right or wrong? but if i do this than result is different

second:

 function Foo(){}
  Foo.prototype={};
 foo=new Foo();

 foo.constructor==Foo//false as aspected

again third:

  function Foo(){}
  Foo.prototype={};
   Foo.prototype.name="Maizere";
 foo=new Foo();

 foo.name==Maizere//true.The definition at the top is applying here,if so why the definition not working in the first: example

plz help with simple english.i m really getting headache.

5
  • Instead of using constructor, couldn't you just use foo instanceof Foo? Commented Jan 27, 2013 at 17:24
  • @FabrícioMatté constructor has no relation with instanceOf operator Commented Jan 27, 2013 at 17:30
  • @FabrícioMatté: That would lead to different problems in here :-/ Commented Jan 27, 2013 at 17:33
  • @Bergi Really? Seems like OP is just trying to check if an instance has the same prototype of a constructor, which instanceof would be ideal for. Unless I'm overlooking something. Commented Jan 27, 2013 at 17:34
  • @OP I know these are "unrelated", however from reading your question it seems like instanceof was what you were looking for in case you're trying to solve an issue. In case this is more of a theoretical question about how the constructor property behaves, then these are unrelated. Commented Jan 27, 2013 at 17:38

2 Answers 2

3

why is this happening

The new operator sets the inheritance of the instance to the object that the constructor's prototype property currently points to. So when you do

function Foo() {}
var foo = new Foo;

it is irrelevant if someone assigns to Foo.prototype after that - it will not change foo.

  • first: The inherited constructor property you are getting comes from the "old" prototype - an object that is initialized together with the function and having that hidden "constructor" property
  • second: You are overwriting the prototype property with an empty object, and your foo (created after overwriting) inherits from that. And since empty objects inherit from Object.prototype, the constructor property now points to the Object function.
  • third: Again, you create foo after overwriting the prototype. Since you assign a name property to that new object from which foo inherits from, you can access it on foo as well.
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your post.But still lacks some more advance answer
but the definition says that the object is set to appoints properties and method which have not been explicitely set in its constructor prototype.so when we appoint new object in the constructor prototype that should also be appointed and tht should make the prototype empty for ever
Makes perfect sense. Visual examples usually help understanding faster so here's a fiddle. What I didn't understand is why the instance doesn't inherit the new prototype object's constructor property.
Oh of course, when you overwrite the prototype with another object, the already created instances are kept unmodified - they inherit from the old prototype. Having a slow day today. Updated fiddle.
@Maizere: Which "definition" are you talking of?
1

When you write a function, it comes with a property named prototype - an object whose constructor property is set to the function itself. As you may have known, JavaScript's object model is prototype based. This makes the objects you create with that function inherit all the properties of the prototype of its constructor function (the one you invoke with new) - so you should beware what prototype is the moment you create the object.

In your first case, you are just setting the prototype property of Foo to an empty object. But the original (default) prototype is still referenced by foo. That explains why the last expression evaluates to true.

By the time you create foo in your second case, prototype of Foo has already been set to an empty object. So virtually foo inherits nothing from it. The same principle applies to the third case.

Comments

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.