简体   繁体   中英

How to access 'this'.method in an object in Javascript?

I have the following code:

function MyFunction () {
   this.my_method1 = function () {};
   this.my_method2 = function () {};
}

How can I access this.my_method1 in this.my_method2. I have tried using:

  • this.my_method1()
  • MyFunction.my_method1()
  • my_method1()

but none seem to work. Help?

The MyFunction function is basically a constructor. Therefore, to access methods you need to create an object out of it:

var foo = new MyFunction();
foo.my_method1();
foo.my_method2();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM