TypeScript笔记二

/**
 * Created by ufo9631 on 2017/5/16.
 */
/*函数新特性*/
function func1() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i - 0] = arguments[_i];
    }
    args.forEach(function (arg) {
        console.log(arg);
    });
}
func1(1, 2, 3);
func1(1, 2, 3, 4, 5, 6, 7);
/*
ES6的语法
function  func2(a,b,c)//...args表示可以传任意数量的参数
{
    console.log(a);
    console.log(b);
    console.log(c);
}
var args=[1,2];
func2(...args);
var args2=[7,8,9,10,11,12];
func2(...args2);
*/
/* ES6的语法
//generator函数,控制函数的执行过程,手工暂停和恢复代码执行
function *doSomething()//声明generator函数
{
 console.log("start");
    yield ;
    console.log("finish");
}
//doSomething();直接调用的话不能实现该功能
var fun3=doSomething(); //必须要这样写
fun3.next();//第一次调用会挂起
fun3.next();//第二次调用继续走

 */
/*析构表达式 通过表达式将对象或数组拆解成任意数量的变量*/
function getStock() {
    return {
        code: "IBM",
        price: 100
    };
}
var _a = getStock(), code = _a.code, price = _a.price; //名称要一样
// var{code:codex,price}=getStock(); 也可以这样写,相当于把code取出来放进codex里
function getStock1() {
    return {
        code: "IBM",
        price: {
            price1: 100,
            price2: 200,
            price3: 300
        }
    };
}
var _b = getStock1(), code = _b.code, price1 = _b.price.price1; //析构表达式嵌套析构表达式
var arr1 = [1, 2, 3, 4, 5];
var number1 = arr1[0], number2 = arr1[1]; //数组的析构表达式拿出1和2
var number1 = arr1[2], number2 = arr1[3]; //拿出3和4
var number1 = arr1[0], number2 = arr1[1], others = arr1.slice(2); //数组的析构表达式拿出1和2剩下的放到others
//------------
function doSomething(_a) {
    var number1 = _a[0], number2 = _a[1], others = _a.slice(2);
    console.log(number1);
    console.log(number2);
    console.log(others);
}
doSomething(arr1);
//------------ 
//# sourceMappingURL=ts3.js.map

  

posted @ 2017-11-15 22:48  编程猴子  阅读(134)  评论(0编辑  收藏  举报