Typescript常见表达式
一、析构表达式:(destructuring)
1.数组析构表达式
用中括号括起来:
var array1 = [1, 2, 3, 4];
function doSomething([number1,number2, ...others]) {
console.log(number1);
console.log(number2);
console.log(others);
}
doSomething(array1);
上述传值过程中,用析构表达式获取数组值,等价于:
var array1 = [1, 2, 3, 4];
var [number1, number2,others] = array1;
2.对象属性析构表达式
用大括号括起来
function getStock() {
return {
code: "IBM",
price: {
price1: 100,
price2:400
}
}
}
var { code:codex, price:{price2} } = getStock();//将获取code属性值存放在codex中
二、箭头表达式
var myArray = [1, 2, 3, 4, 5];
console.log(myArray.filter(value=>value%2==0));
等价于js中的
var myArray = [1, 2, 3, 4, 5];
console.log(myArray.filter(function (value) { return value % 2 == 0; }));