var a=10;
function f(){
var message="hello,world";
return message;
}
function f(){
a=10;
return function g(){
var b=a+1;
return b;
}
}
var g=f();
g();//11

var 声明有一些奇怪的作用域规则

The var declaration has some strange scoping rules

function f(shouldinitalize:boolean){
if(shouldinitalize){
var x=10;
}
return x;
}
f(true)//'10'
f(false)//undefined

多次声明同一个变量并不会报错

Repeated declarations of the same variable do not cause errors

function sumMatrix(matrix:number[][]){
var sum=0;
for(var i=0;i<matrix.length;i++){
var currentRow=matrix[i];
for(var i=0;i<currentRow.length;i++){
sum+=currentRow[i];
}
}
return sum;
}
for(var i=0;i<10;i++){
setTimeout(function(){console.log(i);},100*i);
}
for(var i=0;i<10;i++){
(function(i){
setTimeout(function(){console.log(i);},100*i)
})(i);
}
function f(input:boolean){
let a=100;
if(input){
let b=a+1;
return b;
}
return b;
}

catch语句声明的变量也具有同样的作用域规则

Variables declared by catch statements also have the same scoping rules

try{
throw "wowow"
}catch(e){
console.log("oh well")
}
console.log(e);

重定义及屏蔽

Redefinition and Shielding

function f(x){
var x;
var x;
if(true){
var x;
}
}

块级作用域需要在不用的块里声明

Block-level scopes need to be declared in unused blocks

function f(condition,x){
if(condition){
let x=100;
return x;
}
return x;
}
f(false,0);//return 0
f(true,0)//return 100

在一个嵌套作用域里引一个新的名字称作屏蔽

Lead a new name in a nested scope called shielding

function sumMatrix(matrix:number[][]){
let sum=0;
for(let i=0;i<matrix.length;i++){
var currentRow=matrix[i];
for(let i=0;i<currentRow.length;i++){
sum+=currentRow[i];
}
}
return sum;
}

块级作用域变量的获取

Obtaining Block-level Scope Variables

function theCityThatAlwaysSleeps(){
let getCity;
if(true){
let city="Seattle";
getCity=function(){
return city;
}
}
return getCity();
}
for(let i=0;i<10;i++){
setTimeout(function(){console.log(i);},100*i);
}

const与let声明类似,他们被赋值后不能再改变,他们拥有与let相同作用域规则,凡事不能对他们重新赋值

Like let declarations, const cannot be changed after they are assigned. They have the same scoping

rules as let, and nothing can be reassigned to them.

const numLivesForCat=9;
const kitty={
name:"Aurora",
numLives:numLivesForCat,
}
//Error
kitty={
name:"Danielle",
numLives:numLivesForCat
}
//all okay
kitty.name="Rory";
kitty.name="Kitty";
kitty.name="Cat";
kitty.numLives--;

最简单的解构莫过于数组的解构赋值

The simplest deconstruction is the deconstruction assignment of arrays

let input=[1,2];
let [first,second]=input;
console.log(first);
console.log(second);

解构用于已经声明的变量就更好

It's better to deconstruct variables that have been declared

[first,second]=[second,first];

作用域函数参数

Scope function parameters

function f([first,second]:[number,number]){
console.log(first);
console.log(second);
}
f(input);

使用...语法创建一个剩余变量列表

Create a list of remaining variables using the... Grammar

let[first,...rest]=[1,2,3,4];
console.log(first);
console.log(rest);
//
let [first]=[1,2,3,4];
console.log(first);//1
let[,second,,fourth]=[1,2,3,4];

对象解构

Object deconstruction

let o={
a:"foo",
b:12,
c:"bar"
}
let {a,b}=o;

就像数组解构,可以用没有声明的赋值

Like array deconstruction, assignments without declarations can be used

({a,b}={a:"baz",b:101});

属性命名

Attribute naming

let {a:newName1,b:newName2}=o;
let newName1=o.a;
let newName2=o.b;

属性指示类型

Attribute Indicator Type

let{a,b}:{a:string,b:number}=0;

默认值可以让你的属性为undefined时使用缺省值

Default values allow you to use default values when your attribute is undefined

function keepWholeObject(wholeObject:{a:string,b?:number}){
let {a,b=1001}=wholeObject;
}

函数声明Function declaration

type C={a:string,b?:number}
function f({a,b}:C):void{ }

首先你需要知道在默认值之前设置类型

First you need to know what type to set before the default value

function f({a,b}={a:"",b:0}):void{
//...
}
f()
你需要知道在解构属性上给予一个默认或可选的属性来替换主初始化列表
You need to know that the main initialization list is replaced by a default
or optional attribute on the deconstruction attribute.
function f({a,b=0}={a:""}):void{ }
f({a:"yes"})
f()
f({})

typescript变量声明(学习笔记非干货)的更多相关文章

  1. typescript基础类型(学习笔记非干货)

    布尔值 Boolean let isDone:boolean=false; 数字 Number let decLiteral:number=6; let hexLiteral:number=0xf00 ...

  2. typescript类(学习笔记非干货)

    我们声明一个 Greeter类.这个类有3个成员:一个叫做greeting的属性,一个构造函数和一个greet方法. We declare a Greeter class. This class ha ...

  3. typescript枚举,类型推论,类型兼容性,高级类型,Symbols(学习笔记非干货)

    枚举部分 Enumeration part 使用枚举我们可以定义一些有名字的数字常量. 枚举通过 enum关键字来定义. Using enumerations, we can define some ...

  4. typescript泛型(学习笔记非干货)

    软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型, 这在创建大型系统时为你提供了十分灵活的功能. In softwa ...

  5. typescript接口(学习笔记非干货)

    typescript的核心原则之一就是对所具有的shape类型检查结构性子类型化 One of the core principles of typescript is to check struct ...

  6. mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...

  7. TypeScript 入门教程学习笔记

    TypeScript 入门教程学习笔记 1. 数据类型定义 类型 实例 说明 Number let num: number = 1; 基本类型 String let myName: string = ...

  8. 【转】mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    mongoDB 学习笔记纯干货(mongoose.增删改查.聚合.索引.连接.备份与恢复.监控等等) http://www.cnblogs.com/bxm0927/p/7159556.html

  9. Adaptive AUTOSAR 学习笔记 8 - 干货小结:背景、技术、特征、架构、方法论和 Manifest

    官方文档下载方式及介绍情参见 Adaptive AUTOSAR 学习笔记 2 - 官方文档下载及阅读建议. 这是 Adaptive AUTOSAR 学习笔记的第 8 篇,学习笔记 3 - 7 翻译了 ...

随机推荐

  1. 重建索引解决mssql表查询超时的问题

    表已有数据,150万+,执行一个group by 的查询出现超时,一个一个条件减少尝试,前几个where条件不超时,而在加上最后一个条件时就超时了. 分析表的索引建立情况:DBCC showconti ...

  2. 解决Jira和Confluence访问打开越来越缓慢问题

    Jira和Confluence部署在同一台服务器上,跑一段时间后,发现访问jira和confluence时,打开越来越缓慢.这是因为根据主机物理内存不同,默认的java虚拟机内存也会不同(一个较低值) ...

  3. 基本的排序算法C++实现(插入排序,选择排序,冒泡排序,归并排序,快速排序,最大堆排序,希尔排序)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/8529525.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. Individual Project 1 总结

    题目: http://www.cnblogs.com/jiel/p/3978727.html 1. 估计时间: ① 遍历目录找到所有文本文件 3天 ② 编写统计词频的函数 排序的函数 并输出到文件 2 ...

  5. 个人博客作业Week2(9月30日)

    一.是否需要有代码规范 1.这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 这些规范并不是一开始就有的,也不是由某个人规定的,代码规范是程序员们在不断地编程实践过 ...

  6. Python学习笔记 --第二章

    Python语法基础 "#"号为注释符,建议缩进四个空格,Python大小写敏感. 数据类型 整数 0,2等等,以0x开头的为十六进制数 浮点数 1.58e9 字符串 用'或"括起来的任意文 ...

  7. 对TCP重传的进一步认识

    http://blog.sina.com.cn/s/blog_4d276ac901011ee7.html ——TCM项目所得 一.看图说话 1.基于套接字的TCP服务器/客户端程序流程 2.TCP三次 ...

  8. PAT 1071 小赌怡情

    https://pintia.cn/problem-sets/994805260223102976/problems/994805264312549376 常言道“小赌怡情”.这是一个很简单的小游戏: ...

  9. Selenium的自我总结2_元素基本操作

    对于Selenium的基本元素的操作,就自己的了解做了一个基本的介绍,这篇直接上代码,针对一个页面如何操作写了些基本的操作脚本,希望对初学者有一定的帮助,也希望通过这些总结让自己有一些清晰的认识和了解 ...

  10. QQ互联登录提示redirect uri is illegal(100010)完美解决方法

    大概2015年3月低,腾讯QQ互联开发平台调整了有关QQ登录应用回调地址填写规则,用来修复QQ登录过程因回调地址的漏洞可能导致存在的安全问题. 博主接触这块较多,但也是四月才了解此事,从4月起,所有新 ...