Can you tell me how these three prints work?

<script type="text/javascript">
function Person(name,age,sex){
    Person.prototype.name=name;
    Person.age=age;
    this.sex=sex;
}
var obj=new Person("lisi",18,"male");
console.log(obj.name);  //nameobj.namelisiageobj.ageundefined
console.log(obj.age);//Person.age=ageundefinedthis
console.log(obj.sex);//thissex=malePerson.prototype.sex=sex;
</script>

just learn js, just like the above three prints, I am a little confused about their principle

Jun.28,2021

first you need to take a look at the prototype chain prototype ;

then let's take a look at your questions:

1. Why the name on the prototype can print the lisi directly with obj.name, while the age that is not on the prototype can print undefined? with obj.age.

first of all, you have to understand that the prototype of function prototype is object , in this code:

function Person(name,age,sex){
    Person.prototype.name=name;
    Person.age=age;
    this.sex=sex;
}

the first Person.prototype.name=name; is the name defined on this Person prototype, and the method property on the prototype is the shared by all the Person () new objects used by , so you print out Person.prototype is the following result:

Object: {
    name: 'lisi'
}

and Person.age=age; is a very irregular way of writing? If you need to access it in an object that comes out of new , you can only define its prototype (prototype) .

2. Why Person.age=age printing is undefined, but can be printed on this? Same as above.
3. Why can you print sex=male, with this? how is this different from defining Person.prototype.sex=sex; on a prototype?

first of all, you new a new object obj , then you can see through your constructor that the attribute of the new object coming out of new is sex . this points to the current object, of course, which is true. this is just all the properties of the object . And name you are defined on the Person prototype, this is common to all objects created by Person , this is their most important difference, if you want to know more, please click on the first line of the mdn link.

< hr >

after editing, I found that there was something wrong with your writing, and I couldn't explain it very clearly. We all said that the prototype prototype could not be defined at will. If you define this, it will appear that your concept is not clear. Let me give you an example:

you often use the following method

[1,3,4].push(1);

so where did the push method come from? You can print Array.prototype :
you will find that push this method is defined in Array.prototype , so your new array has the push method, so the prototype definition is this principle. You can compare your Array with your definition of Person , and you should understand.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e59867-441f3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e59867-441f3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?