The relationship between JavaScript constructors, instance objects, and prototype objects

Find the constructor, instance object, and prototype object through the following code


        function Star(uname, age) {
    
    
            this.uname = uname;
            this.age = age;
        }
        Star.prototype.sing = function() {
    
    
                console.log('我会唱歌');
            }
        var ld = new Star('老段', 19);
        console.log(ld);
        ld.sing();
        console.log(Star.prototype);

1. What is a constructor

// Star 就是构造函数     
        function Star(uname, age) {
    
    
            this.uname = uname;
            this.age = age;
        }

2. What is an instance object

            // new Star 是实例对象   只要new了就是实例对象
        var ld = new Star('老段', 19);
        console.log(ld);
        ld.sing();

3. What is a prototype object?


        //  Star.prototype.sing:是原型对象    因为每个构造函数里边都有一个prototype原型对象
        Star.prototype.sing = function() {
    
    
                console.log('我会唱歌');
            }

4. The relationship between the three is
insert image description here
personal summary:
1. There are constructors first, and each constructor has a prototype object; the constructor can execute the prototype object through the prototype
2. There is a constructor attribute in the prototype object prototype, which Point back to the constructor through the constructor attribute
3. Create an instance object through the constructor (as long as there is a new constructor); the instance object has an attribute __protto__ through which it points to the prototype object; the instance object also has an attribute pointing back to the constructor __protto__. constructor.

Guess you like

Origin blog.csdn.net/qq_48203828/article/details/130839205