阅读TS文档中的一些疑问

为什么有了抽象类(abstract)还需要接口(Interface),他们的区别有哪些?

因为抽象类做为其它派生类的基类使用,是基础,是类别。比如界门纲科目属种。而接口是功能的集合,表示要事先预定的功能。接口中的方法都是描述而不能实现,抽象类可以。子类只能有(extends)一个父类,而可以实现(implement)多个接口。

复习下老知识,继承分为哪几步?

  1. 继承父类上的静态属性和静态方法
通过setPrototypeOf(Child, Parent)
  1. 继承父类上的原型属性和原型方法
通过Child.prototype = Object.create(Parent.prototype, {
constructor: Child
})
  1. 继承父类上的实例属性和实例方法
通过在子类构造函数中call或者apply父类。

私有属性是啥,可以被继承吗?

私有属性只能在父类中使用,不能在自己的实例和子类中使用。按规范不可以被继承。

重载在js中的实现?

function double(a:number):number {}
function double(a:string):string {}
function double (x:any){
if  () {
  return 2 * x
} else {
return x+ x
}
}
double(1)
double('aaa')

继承VS多态

子类继承了父类,子类中有更具体的实现。
多个子类继承同一父类,就产生了相关性,对同一个方法有不同的实现就称为多态。

如何描述一个对象的key?

如果要符合条件,必须在函数形参的key中写key:'name'|'age',而不能简单地写string,否则会报错Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Person'. No index signature with a parameter of type 'string' was found on type 'Person'.,可以用keyof拿到Person接口中的属性名称,来作为准确描述

interface Person {
  name: string,
  age: number
}
type K = keyof Person
function attr (obj:Person, key:K) {
  return obj[key]
}
let p3:Person = {name:'asd', age: 12}
attr(p3, 'age')

如何在项目中用ESLINT约束常见错误?

.eslintrc.js

module.exports = {
  "parser":"@typescript-eslint/parser",
  "plugins":["@typescript-eslint"],
  "rules":{
      "no-var":"error",
      "no-extra-semi":"error",
      "@typescript-eslint/indent":["error",2]
  },
  "parserOptions": {
      "ecmaVersion": 6,
      "sourceType": "module",
      "ecmaFeatures": {
        "modules": true
      }
  }
}

安装VS Code插件Eslint
在全局或者项目setting.json中配置

{
  "eslint.autoFixOnSave": true,
  "eslint.validate": [
      "javascript",
      "javascriptreact",
      {
          "language": "typescript",
          "autoFix": true
      },
       {
          "language": "typescriptreact",
          "autoFix": true
      }
  ]
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容