ts


https://www.tslang.cn/docs/handbook/interfaces.html






是否name属性变成了联合属性?

















get/set存储器
1、存取器要求你将编译器设置为输出ECMAScript 5或更高。 不支持ECMAScript 3。 编译要使用 tsc 文件名.ts -t es5
2、只带有 get不带有 set的存取器自动被推断为 readonly
tatic属性,不会被实例化的属性 ,我感觉和private差不多
class Animal{
private name:string
private size:string = "small"
legs: number = 4
constructor(animalName:string){
this.name = animalName
}
getLegs():number{
return this.legs
}
get allName(){
return this.name
}
set allName(theName:string){
this.name = theName
}
get allSize(){
return this.size
}
}
let cat = new Animal("Cat")
console.log("这只动物是: " + cat.allName) //注意存储器的调用方法,不加()
console.log("有 " + cat.getLegs() + "条腿") // 方法要加()
console.log("这只动物的大小是: " + cat.allSize)
cat.allName = "Dog" // 通过存储器来修改name
// cat.allSize = "Big"
// 上面这行报错
// 无法通过存储器来修改size,因为allsize只有get,没有set,就是只读的
// Cannot assign to 'allSize' because it is a read-only property.
console.log("这只动物现在是: " + cat.allName)
console.log("这只动物现在的大小 仍然 是: " + cat.allSize)

react里的ts参考:https://juejin.im/post/5ce9f69ae51d45106b15fe98
https://juejin.im/post/5bed5f03e51d453c9515e69b
https://juejin.im/post/5d0259f2518825405d15ae62#heading-17
ts小demo:https://juejin.im/post/5ceddb8c6fb9a07ef201029e
https://juejin.im/post/5d10cc1f6fb9a07eaf2b9db6
参考:https://juejin.im/post/5ce24f8ae51d45106477bd45





浙公网安备 33010602011771号