A
is your constructor, that means you put in that function everything you need to performe on the newly created object when you "initialize" it
in your case you do nothing.
the constructor has a prototype property which is the object that all the object you will create by new A()
will inherit from
if you don't explicitly set a prototype to your constructor so your prototype is an empty object by default.
so at that line : const c = new A();
c inherites an empty object
when you set :
A.prototype.sex = "girl";
you are creating a property sex on your empty object and you assign to it "girl"
but that object is the same (same reference) as before you do "new". So c
still has a reference to it
BUT when you do:
A.prototype={
name:"q",
age:12
};
you change the prototype of A i.e you change the reference of A.prototype, but you didn't use new anymore. So there is no object that actually has
{
name:"q",
age:12
}
as prototype. c is still having the former object (remember the empty) object as prototype.
But if you do : const d = new A()
then, d.name
will existe but d.sex
won't
changing the prototype as you did (assigning another reference to the A.prototype) just affects A.prototype not the objects that have been 'instanciated' before that affectation.