1.重载:使用相同名称和不同参数数量或类型创建多个方法
2. 通常约定,TypeScript 和 JavaScript开发者使用下划线(_)开始的变量名座位私有变量名。
3.IIFE: 立即调用函数表达式
(function() {
})();
4. 模板字符串
var name = 'remo';
var surname = jansen;
var html = '<hl>${name} ${surname} </h1>';
5. 回调函数 :
var foo = function(){} //回调函数
function bar(cb : () => void) { //高阶函数:函数中以别的函数作为参数的叫高阶函数。 =>void 回调函数没有返回值
cb();
}
6. this需要谨慎使用,当定义一个异步函数(包含回调)时,this关键词会改变它的指向值,指向匿名函数。
例:class Person{
name : string;
constructor(name : string){ this.name = name}; //构造函数
greet(){ alert('Hi ! My name is ${this.name}');}
greetDelay(time:number){
setTimeout(function(){ alert('Hi! My name is ${this.name}');}, time); //立即调用函数
}
}
var remo = new Person("remo");
remo.greet(); //Hi ! My name is remo
remo.greetDelay(1000);//Hi! My name is //greetDelay延时回调,this指向了别处,所以取不到name
箭头函数会绑定this操作符,使用箭头函数就不需要担心this的指向问题了。
greetDelay( time : number){
setTimeout( () => { alert('Hi! My name is ${this.name'}); }, time);
}
上面的函数编译后会生成这样:
Person.prototype.greetDelay = function (time){
var _this = this;
setTimeout(function () {
alert("Hi! My name is " + _this.name);
}, time);
};
所以this依旧是指向person的。