Looking back, lesson 8-2, constructors

Time:2020-3-25

My own code

<span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> Fuck() {
        </span><span style="background-color: #f5f5f5; color: #0000ff;">this</span><span style="background-color: #f5f5f5; color: #000000;">.c</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">{
            ff:</span><span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> () {
                </span><span style="background-color: #f5f5f5; color: #0000ff;">return</span><span style="background-color: #f5f5f5; color: #000000;"> {
                    hi:</span><span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> () {
                        </span><span style="background-color: #f5f5f5; color: #0000ff;">return</span> <span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">.</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">
                    }
                }
            }
        }
    }
    </span><span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> gz</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #0000ff;">new</span><span style="background-color: #f5f5f5; color: #000000;"> Fuck();
    console.log(gz.c.ff().hi());
</span>

Constructor is very simple. Let’s talk about this

This represents the new object after the constructor is instantiated. Here, only this in the constructor is described, not this in other places

The implementation of the constructor is the process of continuously assigning properties and methods to the new object this.

 

var Cteate = function () {
        this.a="11";
    };

    //Add static member
    Ctate. STR = "hello";

    //Accessing static members
    console.log(Cteate.str);

Static members can’t use instantiated objects to call properties. They want to use instantiated objects to call properties, that is, to share a property calledPrototype property

 

var Create = function () {
        this.a="11";
    };

    //Add static member
    Create. STR = "hello";

    //Accessing static members
    console.log(Create.str);

    Var obj = new create(); // add a prototype property with prototype to share this property
    Create.prototype.ok=function () {
        return "ok ok ";
    };
    var obj2=new Create();
    console.log(obj.ok());
    console.log(obj2.ok());