The resource loading... loading...

Using the _C function on a custom class member function does not get the correct this

Author: lightring, Created: 2019-08-11 14:47:22, Updated: 2019-08-11 14:50:47

Try running the following code:

function MyClass()
{
    this.m_name = "my name";
}

MyClass.prototype.printMyName = function()
{
    Log(this.m_name);
    return true;
}

function main() {
    var myobj = new MyClass();
    myobj.printMyName();
    _C(myobj.printMyName);
}

The result is:

  • my name
  • null

This does not point to the myobj object in the function when _C ((myobj.printMyName) is called. How can we solve this problem?


More

Inventors quantify - small dreams ``` function MyClass() { var self = {} self.m_name = "My Name" self.printMyName = function () { Log(self.m_name) return true } return self } function main() { var myobj = MyClass() myobj.printMyName() _C(myobj.printMyName) } ```

Inventors quantify - small dreamsThe reason is that after myobj.printMyName entered _C, the this pointer pointed to ▽ changed.

lightringThank you!