gpt4 book ai didi

angular - 为什么我们不在 Angular 组件中使用关键字 'function'?

转载 作者:行者123 更新时间:2023-12-03 17:05:23 24 4
gpt4 key购买 nike

在 ts 文件中,

export class ComponentName{
methodName (para1,para2) {
//code here
}
}

这被认为是正确的,但是当我将关键字 function 添加到方法时,代码不会编译并给出错误,说它只需要方法、访问器、构造函数和属性。

最佳答案

我认为您在理解 ECMAScript 6 时遇到了问题。Angular 使用基于 ECMAScript 6 的 TypeScript。

Traditional JavaScript uses functions and prototype-based inheritance to build up reusable components, but this may feel a bit awkward to programmers more comfortable with an "object-oriented approach", where classes inherit functionality and objects are built from these classes. Starting with ECMAScript 2015, also known as ECMAScript 6, JavaScript programmers will be able to build their applications using this object-oriented class-based approach. In TypeScript, we allow developers to use these techniques now, and compile them down to JavaScript that works across all major browsers and platforms, without having to wait for the next version of JavaScript.

...

class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}

let greeter = new Greeter("world");

The syntax should look familiar if you’ve used C# or Java before. We declare a new class Greeter. This class has three members: a property called greeting, a constructor, and a method greet.



[ Source ]

关于angular - 为什么我们不在 Angular 组件中使用关键字 'function'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44476764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com