字符串
多行字符串
使用“,可以在字符串中换行。
var line = `abc
def`
字符串模板
在“中,可以使用${}引入变量。
var myname = "zhai liang"
var getName = function () {
return "zhai liang";
}
console.log(`hello ${myname}`);
console.log(`hello ${getName()}`);
自动拆分字符串
可以用字符串模板调用方法。
function test(template, name, age) {
console.log(template);
console.log(name);
console.log(age);
}
var myname = "zhai liang"
var getAge = function(){
return 18;
}
test`hello my name is ${myname}, my age is ${getAge()}`
参数
参数类型
var myname: string = "name";
var alias: any = "hello";
var age: number = 13;
var man: boolean = true;
//函数返回值类型
function test(): void{
}
function test2(name: string): string{
return "";
}
//自定义类型
class Person{
name: string;
age: number;
}
var zhangsan: Person = new Person();
zhangsan.name = "zhangsan";
zhangsan.age = 18;
参数默认值
function test(a: string, b: string, c: string = "c") {
console.log(a);
console.log(b);
console.log(b);
}
//指定了默认值后,可以只传两个参数
test("x", "y")
//带默认值的参数只能声明在最后面
function test2(a: string = "a", b: string, c: string) {
console.log(a);
console.log(b);
console.log(b);
}
可选参数
参数加上?,变成可选参数
可选参数不能在必填参数后面
function test(a: string, b?: string, c: string = "c") {
console.log(a);
console.log(b);
console.log(b);
}
test("x")
Rest and Spread
function func1(...args) {
args.forEach(function (arg) {
console.log(arg);
})
}
func1(1, 2, 3)
function func2(a, b, c) {
console.log(a);
console.log(b);
console.log(c);
}
var args = [1, 2]
func2(...args)
generator
第一次调用next,会停在yield函数。第二次调用,才会继续打印finish。
function doSomething() {
console.log("start");
yield;
console.log("finish");
}
var func1 = doSomething();
func1.next();
func1.next();
析构表达式
通过表达式,将对象或数组,拆解成任意数量变量。
function getStock(){
return {
code: "IBM",
price: {
price1: 100,
price2:400
}
}
}
var { code: codex, price: { price2} } = getStock();
console.log(codex)
console.log(price2)
表达式与循环
箭头表达式
用来声明匿名函数,消除传统匿名函数的this指针问题。
//两个参数
var sum = (arg1, arg2) => arg1 + arg2;
var sum = (arg1, arg2) => {
return arg1 + arg2;
}
//只有一个参数
var sum2 = arg1 => {
console.log(arg1);
}
//取出数组中偶数
var myarray = [1, 2, 3, 4, 5]
console.log(myarray.filter(value => value % 2 == 0));
循环
//forEach
var myarray = [1, 2, 3, 4, 5]
myarray.desc = "four number"
myarray.forEach(value => console.log(value));
//for in 打印出数组集合下标和属性
for (var n in myarray) {
console.log(n);//0 1 2 3 4 four number
}
//for of 打印出数组内容
for (var n in myarray) {
console.log(n);//1, 2, 3, 4, 5
}
类
类的声明
class Person{
public name;
public eat() {
console.log("I'm eating");
}
}
var p1 = new Person();
p1.name = "name1";
p1.eat();
var p2 = new Person();
p2.name = "name2";
p2.eat();
构造函数
class Person{
constructor(name: string) {
this.name = name;
console.log("constructor");
}
public name;
public eat() {
console.log("I'm eating");
}
}
var p1 = new Person("name1");
p1.eat();
var p2 = new Person("name2");
p2.eat();
类的继承
class Person{
constructor(name: string) {
this.name = name;
console.log("constructor");
}
public name;
public eat() {
console.log("I'm eating");
}
}
class Employee extends Person{
constructor(name:string, code:string) {
super(name);
}
code: string;
work() {
super.eat();
this.doWork();
}
private doWork() {
console.log("I am working");
}
}
var e1 = new Employee("name1","1");
泛型
class Person{
constructor(name: string) {
this.name = name;
console.log("constructor");
}
public name;
public eat() {
console.log("I'm eating");
}
}
class Employee extends Person{
constructor(name:string, code:string) {
super(name);
}
code: string;
work() {
super.eat();
this.doWork();
}
private doWork() {
console.log("I am working");
}
}
//泛型:指定数组只能放指定类型的元素,不能放其他类型的元素
var workers: Array<Person> = [];
workers[0] = new Person("zhangsan");
workers[1] = new Employee("lisi", "2");
接口
用来建立某种代码约定,使得其他开发者在调用某个方法或创建新的类时必须遵循接口所定义的代码约定。
interface IPerson{
name: string;
age: number;
}
class Person{
constructor(public config: IPerson) {
}
}
var p1 = new Person({
name: "zhangsan",
age: 18
});
interface Animal{
eat();
}
class Sheep implements Animal{
eat() {
console.log("i eat grass");
}
}
class Tiger implements Animal{
eat() {
console.log("i eat meat");
}
}