Skip to main content

35 posts tagged with "前端核心底座"

View All Tags

TypeScript Interface: Extend or implement

· 2 min read
Kimi Gao
Fullstack & AI

Interface extends interface

Interfaces can extend one or more interfaces. This makes writing interfaces flexible and reusable.

interface Person {
name: string;
gender: string;
}

interface Employee extends Person {
department: string;
}

let empObj: Employee = {
name: 'Kimi',
gender: 'male',
department: 'Payment',
};

Type Script Interface: As Object Or Array Type

· 6 min read
Kimi Gao
Fullstack & AI

Interface in TypeScript can be used to define a type and also to implement it in the class. The most scenarios can be summarized as following:

  • as object type definition
  • as array type definition
  • as function type definition
  • interface extends interface
  • class implements interface

In this section, we mainly focus on the object and array type definition using interface. As function type definition will be introduced in Function: Types section.

Interface likes contract which defines the structure of the object, array, function and also class. However, we need to note that the TypeScript compiler does not convert interface to JavaScript. It uses interface for type checking.

TypeScript tsconfig.json Usage

· 25 min read
Kimi Gao
Fullstack & AI

TypeScript项目中,tsconfig.json文件指定了用来编译这个项目的根文件和编译选项,通过自定义tsconfig.json文件中的配置项,可以达到我们想要的编译结果。

tsc

当我们使用tsc命令对项目进行编译时,编译器会从当前目录开始去查找tsconfig.json文件,逐级向上搜索父目录。

下面我们将通过以下三个方面来讲述tsconfig.json配置:

  • 文件选项: filesincludeexclude
  • 编译选项: compilerOptions
  • 项目引用: extendsreferences

文件选项

files

files指定一个包含相对或绝对文件路径的列表,列举在files中的所有文件,编译器在编译时都会将它们包含在内。

// tsconfig.json
"files": [
"src/core.ts",
"src/index.ts",
]

当配置文件中的files字段值如上所示时,使用tsc命令编译时,src目录下的core.tsindex.ts文件会被编译为core.jsindex.js文件。

Javascript setTimeout & setInterval

· 16 min read
Kimi Gao
Fullstack & AI

有时我们并不想立即执行一个函数,而是等待特定一段时间之后再执行,目前有两种方式可以实现:

  • setTimeout 将函数的执行推迟到一段时间之后再执行。
  • setInterval 让函数间隔一定时间周期性执行。

这两个方法并不存在于 JavaScript 的规范中。但是大多数运行环境都有内置的定时器,而且也提供了这两个方法的实现。目前来讲,所有浏览器,包括 Node.js 都支持这两个方法。

JavaScript 节点增删改查

· 13 min read
Kimi Gao
Fullstack & AI

查找节点

前面四个方法除了定义在 document 对象上,还定义在 Element 上,即在元素节点上也可以调用。

querySelector()

document.querySelector 方法接受一个 CSS 选择器作为参数,返回匹配该选择器的元素节点。如果有多个节点满足匹配条件,则返回第一个匹配的节点。如果没有发现匹配的节点,则返回null

let el1 = document.querySelector('.myclass');
let el2 = document.querySelector('#myParent > [ng-click]');

Since HTML5 it’s been valid to start an HTML element ID with a number. For example <div id="10">. From an HTML perspective, that’s fine.

JavaScript 事件系统

· 14 min read
Kimi Gao
Fullstack & AI

事件系统概述

基础概念

JavaScript 和 HTML 之间的交互是通过事件来实现的。事件,就是文档或浏览器窗口之间发生的一些交互瞬间。可以使用侦听器(或处理程序)来监听事件,以便事情发生时执行相应的代码。

一个完整的事件系统,通常存在以下三个角色:

  • 事件对象,用于储存事件的状态。
  • 事件源对象,当前事件在操作的对象,如元素节点,文档对象,window 对象,XMLHttpRequest 对象等。
  • 事件监听函数,当一个事件源生成一个事件对象时,它会调用相应的回调函数进行操作。

通俗点讲,事件源对象相当于”当事人“,事件监听函数相当于”监护人“,事件对象相当于”事故详情“。一个事件可以理解为,当事人出了点事,至于什么事情(被打了,还是被抢了)都记录在事故详情里,监护人根据事故详情得做出点反应(回调函数)。