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
How to access a method from a class from another class?
提问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 one
and 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 One
object in a constructor for Two
like 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 One
instead 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 Two
class because you can pass in a mock implementation of the One
class 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;
});
});