简体   繁体   English

如何访问 'this'.method 在 Javascript 的对象中?

[英]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.如何在 this.my_method2 中访问 this.my_method1。 I have tried using:我试过使用:

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

but none seem to work.但似乎没有一个工作。 Help?帮助?

The MyFunction function is basically a constructor. MyFunction函数基本上是一个构造函数。 Therefore, to access methods you need to create an object out of it:因此,要访问方法,您需要从中创建一个对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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