Typescript
工具 vscode
Installation
windows
下载安装文件,按提示安装,如果出现异常情况,可以尝试关掉防火墙、重启系统等常用windows异常处理办法。
自定义配置 Configuration
个人设置
菜单 File->Preferences->Settings(默认快捷键ctrl+,),打开左右两个分开的窗口,左边为默认设置,不能修改,右边为个人设置,个人设置优先级高于系统默认设置,为JSON格式,推荐设置
{
"editor.snippetSuggestions": "none",
"editor.tabCompletion": true,
"editor.fontSize": 20,
"editor.insertSpaces": false,
"editor.wordWrap": "bounded",
"editor.detectIndentation": false,
"editor.fontLigatures": true,
"editor.renderWhitespace": "all",
"editor.minimap.renderCharacters": false,
"editor.minimap.showSlider": "always",
"editor.formatOnSave": true,
"files.trimTrailingWhitespace": true,
"files.autoSave": "onFocusChange",
"git.enableSmartCommit": true
}
快捷键
如果快捷键与个人使用习惯不一致,可以在 菜单File->Preferences->Keybord Shortcuts 或 Keymap Extentions中修改
多行编辑 MultiEdit
修改多处文本
选中一处文本,按快捷键ctrl+d选中多处相同的文本
多行编辑
ctrl+shift+方向键可同时选中多行进行编辑
开发语言 TypeScript
箭头函数 Arrow Function
箭头函数主要用来解决this问题,为了避免this问题,推荐尽可能地使用箭头函数
为了主义明确,不推荐省略参数处的括号,不省略return
// 不推荐的写法
[1, 2, 3].map(it => ++it;});
// 推荐的写法
[1, 2, 3].map((it) => {
return ++it;
});
函数 Function
const add = new Function('a', 'b', 'return a + b;');
add(1, 2); // 3
自执行函数(立即执行函数)
((a: number, b: number) => {
return a + b;
})(1, 2);
闭包
注意下例中变量r的变化
const add = (() => {
let r = '';
return (str) => {
return r += str;
};
})();
add('hello'); // hello
add(' world'); // hello world
Class & Interface
class
与其它面向对象的类概念相似,interface
与其它面向对象的语言稍有不同,也是非常容易掌握的。
function printLabel(labelledObj: { label: string }) {
console.log(labelledObj.label);
}
let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);
函数式编程
函数作为参数
function add(a: () => number, b: () => number) {
return a() + b();
}
函数作为返回值
function add(a: number) {
return (b: number) => {
return a + b;
}
}
练习
两个数相加,写出尽可能多的方法,完成后学习测试项目
完成情况
Name | Remarks ——|——–
备注
函数作为参数及返回值的情况各位同学均未涉及。