Javascript 如何从另一个类访问一个类的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39175922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:18:28  来源:igfitidea点击:

How to access a method from a class from another class?

javascriptoop

提问by Hamdi Bayhan

I want to use Object Oriented Programming technique with JavaScript but I can't access a method from one class from another class. How can do like the following?

我想在 JavaScript 中使用面向对象的编程技术,但我无法从另一个类访问一个类的方法。怎么能像下面这样?

class one{

  write(){
    console.log("Yes! I did!");
  }
}

class two{
   var object=new one();

   tryingMethod(){
   object.write();
   }
}

I get the following error:

我收到以下错误:

Uncaught SyntaxError: Unexpected identifier -->> for var object=new one();

Uncaught SyntaxError: Unexpected identifier -->> for var object=new one();

回答by jfriend00

Your syntax is not legal. There should be an error in your console showing you which line of code is not correct.

你的语法不合法。您的控制台中应该有一个错误,向您显示哪行代码不正确。

If it's a static method (doesn't use any instance data), then declare it as a static method and you can directly call it.

如果是静态方法(不使用任何实例数据),则将其声明为静态方法,您可以直接调用它。

If it's an instance method, then you would typically create an object of type oneand then call the method on that object (usually in the constructor).

如果它是一个实例方法,那么您通常会创建一个类型的对象,one然后在该对象上调用该方法(通常在构造函数中)。

To make the method static (which appears to be fine in your specific case):

使方法静态(在您的特定情况下似乎没问题):

class One {
  static write(){
    console.log("Yes! I did!");
  }
}

class Two {
   tryingMethod(){
     One.write();
   }
}

For the non-static case, you don't have the proper syntax. It appears you want to create the instance of the Oneobject in a constructor for Twolike this:

对于非静态情况,您没有正确的语法。看来您想One在构造函数中创建对象的实例,Two如下所示:

class One {
  write(){
    console.log("Yes! I did!");
  }
}

class Two {
   constructor() {
       this.one = new One();
   }

   tryingMethod(){
     this.one.write();
   }
}

var x = new Two();
x.tryingMethod();

Note: I'm also following a common Javascript convention of using an identifier that starts with a capital letter for the class/constructor name such as Oneinstead of one.

注意:我还遵循一个常见的 Javascript 约定,即使用以大写字母开头的类/构造函数名称的标识符,例如One代替one.

回答by jcreamer898

What I'd recommend doing is not tying the classes together so tightly and doing something like this...

我建议做的不是将课程紧密地联系在一起并做这样的事情......

class One {
  write() {
    console.log("Yes! I did!");
  }
}

class Two {
  constructor(one = new One())  {
    this.one = one;
  }
  tryingMethod() {
    this.one.write();
  }
}

Now what you can do is...

现在你能做的就是...

const two = new Two();
two.write();

This allows you to have a better separation of concerns and allows you to more easily unit test the Twoclass because you can pass in a mock implementation of the Oneclass if you want.

这使您可以更好地分离关注点,并允许您更轻松地对Two类进行单元测试,因为您可以根据需要传入类的模拟实现One

describe("two class", () => {
  it("should do something", () => {
    const one = {
      write: sinon.spy()
    };
    const two = new Two(one)

    two.tryingMethod();

    expect(one.write.calledOnce).to.be.ok;
  });
});