typescript 打字稿:调用另一个类的“方法”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35557365/
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
Typescript: Calling a "method" of another class
提问by Harald Thomson
I'm pretty new to java-/type-script and I've some troubles grasping their concepts. I would like to call a methodof another class. However, I've been unsuccessful so far.
我对 java-/type-script 很陌生,我在理解他们的概念时遇到了一些麻烦。我想调用另一个类的方法。但是,到目前为止,我一直没有成功。
export class Foo {
calcSomeThing(parameter:number): number {
//stuff
}
}
class Bar {
var foo:Foo = new Foo();
calcOtherThing() {
result = foo.calcSomething(parameter)
}
}
What is the correct way to call calcSomething
on foo
from calcOtherThing
?
什么是调用正确的方式calcSomething
上foo
从calcOtherThing
?
edit: added an instance of foo
编辑:添加了一个实例 foo
回答by Amid
There are several problems with your code.
您的代码有几个问题。
- Typescript is case sensitive. So "calcSomething" and "calcSomeThing" are two different methods.
- The only way to access cals methods and properties is through "this" keyword: this.foo
- To define class property use private/protected/public modifier. Or no modifier at all (that will be the same as public). So no things like "var foo" in the class body.
- 打字稿区分大小写。所以“calcSomething”和“calcSomeThing”是两种不同的方法。
- 访问 cals 方法和属性的唯一方法是通过“this”关键字:this.foo
- 要定义类属性,请使用 private/protected/public 修饰符。或者根本没有修饰符(这将与 public 相同)。所以在类体中没有像“var foo”这样的东西。
Taking this into account the fixed code would look like this:
考虑到这一点,固定代码将如下所示:
export class Foo
{
calcSomeThing(parameter:number): number
{
//Stuff
}
}
class Bar
{
private foo:Foo = new Foo();
calcOtherThing(parameter: number): number
{
return this.foo.calcSomeThing(parameter)
}
}
回答by dex
calcSomeThing
is a non-static method/function. Create an instance of Foo
to be able to call it:
calcSomeThing
是一个非静态方法/函数。创建一个实例Foo
以便能够调用它:
let foo:Foo = new Foo();
let result:number = foo.calcSomeThing( parameter );
Never use var
in Typescript - let
is your friend.
永远不要var
在 Typescript 中使用-let
是你的朋友。
回答by tzipp
I believe you need a constructor for classes in TypeScript. In the example I provide I made mine data holders, but it's not required. Additionally, your calculation functions need to return values. Also, in order to use Foo in an instance of Bar, you need to make an instance of Foo.
我相信你需要一个 TypeScript 类的构造函数。在我提供的示例中,我制作了我的数据持有者,但这不是必需的。此外,您的计算函数需要返回值。此外,为了在 Bar 的实例中使用 Foo,您需要创建一个 Foo 的实例。
class Foo {
private data;
constructor(data: number) {
this.data = data;
}
calcSomeThing(parameter:number): number {
return parameter + 1;
}
}
class Bar {
private data;
private foo:Foo = new Foo(3);
constructor(data: number) {
this.data = data;
};
calcOtherThing(): number {
let result = this.foo.calcSomeThing(this.data);
return result;
}
}
let bar = new Bar(5);
console.log(bar.calcOtherThing()); // returns 6
回答by Joel Balmer
It may not be right for all situations, but for the angular app I'm working on, I'm been using a service - here's what angular says about them. You can then call them like this:
它可能并不适用于所有情况,但对于我正在开发的 angular 应用程序,我一直在使用服务 -这是 angular 对它们的说明。然后你可以这样调用它们:
smile.service.ts
微笑服务.ts
export class SmileService {
addSmileMethod(input: string): string {
return input + ' :)';
}
}
smile-component.ts
微笑组件.ts
import { SmileService } from './path/to/smile.service';
export class SmileComponent {
constructor(private smileService: SmileService) { }
ngOnInit() {
// Using the service
const smileString = this.smileService.addSmileMethod('Hello!');
console.log(smileString);
// Output is:
// Hello! :)
}
}
回答by jmunsch
Here's another example, but with a shared exported method.
这是另一个示例,但使用共享导出方法。
a.ts
:
a.ts
:
export function sharedMethod(a, b, c) { return a + b + c }
export default class A {
constructor(a, b, c) {
this.concat = sharedMethod(a,b,c);
};
}
And in b.ts
:
并在b.ts
:
import { sharedMethod } from './a'
export default class B {
constructor(a, b, c) {
this.concat = sharedMethod(a,b,c)
};
}
and in c.ts
:
并在c.ts
:
import './a'
import './b'
new A('hello', 'world', '!')
new B('hello', 'world', '!')