On the problem of constructor

function Parent() {

}

Parent.prototype.func1 = function(callback) {
    this.callback = callback;
}
Parent.prototype.func2 = function() {
    this.callback.fetchData()
}

in the constructor, whether it is an attribute defined by func1, such as this.callback. Can you also access it in func2?

Feb.28,2021

Yes. The this in the prototype and constructor both point to the instance to be created in the future


. After instantiation, you can use the obtained object to execute func1, once you call func2 in this object, and you can access thi.callback


.

it is possible that the this in the constructor is an instance object, and these properties are hung on the instance object.

function Parent() {

}

Parent.prototype.func1 = function(callback) {
    this.callback = callback;
}
Parent.prototype.func2 = function() {
  console.log(this.callback)
    this.callback.fetchData()
}



Parent.prototype.func1({fetchData: function(){
    console.log(2)
}})

let per = new Parent()
per.func2()

clipboard.png


@Captain @Just_do chromethisParent

after chrome breakpoint testing, it is not because there is no new Parent () instantiation that causes this to point to Parent. On the attribute of the prototype of the constructor, this refers to the Parent. (make a comment for next time)

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-1b30be0-2bd50.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-1b30be0-2bd50.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?