- Typescript 中的public、protected、private与Java用法一致
- Javascript 没有域的修饰符,typescript只在编写时有用,Typescript的代码需要转成JS才能被brower加载。也就是说你在Typescript设计了很多,最终是没有意义的。
(()=>{
class Person{
protected name:string
protected constructor(name:string){
this.name = name
}
public eat(food:string) {
console.log(this.name+" likes eating "+food)
}
}
class Student extends Person{
public constructor(name:string){
super(name)
}
public study(book:string){
console.log(this.name+" likes reading "+book)
}
}
const stu = new Student("Michael")
stu.eat('penut')
stu.study('math and English')
}
)()
转换后的JS代码:
(() => {
class Person {
constructor(name) {
this.name = name;
}
eat(food) {
console.log(this.name + " likes eating " + food);
}
}
class Student extends Person {
constructor(name) {
super(name);
}
study(book) {
console.log(this.name + " likes reading " + book);
}
}
const stu = new Student("Michael");
stu.eat('penut');
stu.study('math and English');
})();