Determine whether the property function in the instance of the JS constructor occupies the same piece of memory

problem description

what is the difference between the function declared by function inside the JS constructor and the function declared by this, and why the function declared by function still points to the same memory after instantiation, while the function declared by this points to the same memory

the environmental background of the problems and what methods you have tried

console.log () output problem

related codes

/ / Please paste the code text below (do not replace the code with pictures)

function Person(age,name) {
            this.age = age;
            this.name = name;
            function showSex() {
                console.log("girl");
            }
            this.say = function() {}
        }  
        var p1 = new Person();
        var p2 = new Person();
        console.log("showSex",p1.showSex === p2.showSex);  
        console.log("say",p1.say === p2.say);  
        Person.prototype.run = function () {
            console.log("1");
        }
        console.log("run",p1.run === p2.run);
        console.log("run",p1.run === Person.prototype.run);
        // 
What is the error message that

actually sees?

The output of

is true,console.log ("showSex function", p1.showSex = p2.showSex); / / true

Jun.27,2022

p1.showSex = p2.showSex is actually
undefined = undefined,//true

clipboard.png


explain in the order in which you print.
1, console.log ("showSex function", p1.showSex = p2.showSex);
this output is true, because p1.showSex is both undefined,p2.showSex and undefined.
is not because the function declared by function still points to the same piece of memory after instantiation.

2, console.log ("say function", p1.say = = p2.say).
this is false, and each of the two objects has its own say function, which is not equal.
explain why: when you use new to generate a new object, the system performs the following steps when calling the constructor.
(1) creates a new object;
(2) assigns the scope of the constructor to the new object (so this points to the new object, and one of the two steps is equivalent to this = {});
(3) executes the code in the constructor (adding properties to the new object);
(4) returns the new object (return this).

3, console.log ("run function", p1.run = p2.run); / / true
this is a function that run defines on the prototype object of Person and shared by all new Person () instances.
the knowledge of prototypes and prototype chains can be consolidated.

4, console.log ("run function", p1.run = Person.prototype.run); / / true
is the same as 3, run originally points to the run function on the Person prototype object.

I wonder if the landlord has any other questions.


The function declared by default in

Person:

 function showSex() {
    console.log("girl");
}p1.showSex  undefined
this.say = function() {}say
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-1bf2095-31618.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-1bf2095-31618.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?