04.TypeScript函数新特性

  1. Rest and Spread操作符
    任意数量的方法参数,restAndSpread.ts文件内容如下
    function test(...args ){
        args.forEach(function(arg){
            console.log(arg);
        });
    }
    
    test('aaa', 'bbb');
    test('aaa');
    
    function test2(a, b, c) {
        console.log(a);
        console.log(b);
        console.log(c);
    }
    
    var arr1 = [1, 2];
    var arr2 = [7, 8, 9, 10];
    test2(...arr1);
    test2(...arr2);
    
      编译后的restAndSpread.js文件内容如下
    function test() {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        args.forEach(function (arg) {
            console.log(arg);
        });
    }
    test('aaa', 'bbb');
    test('aaa');
    function test2(a, b, c) {
        console.log(a);
        console.log(b);
        console.log(c);
    }
    var arr1 = [1, 2];
    var arr2 = [7, 8, 9, 10];
    test2.apply(void 0, arr1);
    test2.apply(void 0, arr2);
     
  2. generator函数
    实例1:generator1.ts文件内容如下
    function* doSomething() {
        console.log('start');
        yield;
        console.log('finish');
    }
    
    var func1 = doSomething();
    func1.next();
    func1.next();
    
    实例2:generator2.ts文件内容如下
    function* getStockPrice(stock) {
        while (true) {
            yield Math.random()*100;
        }
    }
    
    var priceGenerator = getStockPrice("IBM");
    
    var price = 100;
    var limitPrice = 15;
    
    while (price > 15) {
        price = priceGenerator.next().value;
        console.log(`the generator return ${price}`)
    }
    
    console.log(`buying at ${price}`);
      
  3. 析构表达式
    对象的析构表达式,objectDestructuring.ts文件的内容如下
    function getStock() {
        return {
            code: '3938168',
            name:'IBM',
            price: {
                price1: 200,
                price2:400
            }
        }
    }
    
    var { code, name: stockName, price: { price2 } } = getStock();
    
    console.log(code);
    console.log(stockName);
    console.log(price2);
    编译后objectDestructuring.js的文件内容如下
    function getStock() {
        return {
            code: '3938168',
            name: 'IBM',
            price: {
                price1: 200,
                price2: 400
            }
        };
    }
    var _a = getStock(), code = _a.code, stockName = _a.name, price2 = _a.price.price2;
    console.log(code);
    console.log(stockName);
    console.log(price2);
    数组的析构表达式,arrayDestructuring.ts文件的内容如下
    var arr = [1, 2, 3, 4];
    var [number1, , , number2] = arr;
    console.log(number1);
    console.log(number2);
    
    arr = [5, 6, 7, 8];
    var [n1, n2, ...others] = arr;
    console.log(n1);
    console.log(n2);
    console.log(others);
    
    function test([n1,n2,...others]) {
        console.log(n1);
        console.log(n2);
        console.log(others);
    }
    
    test(arr);
    编译后arrayDestructuring.js的文件内容如下  
    var arr = [1, 2, 3, 4];
    var number1 = arr[0], number2 = arr[3];
    console.log(number1);
    console.log(number2);
    arr = [5, 6, 7, 8];
    var n1 = arr[0], n2 = arr[1], others = arr.slice(2);
    console.log(n1);
    console.log(n2);
    console.log(others);
    function test(_a) {
        var n1 = _a[0], n2 = _a[1], others = _a.slice(2);
        console.log(n1);
        console.log(n2);
        console.log(others);
    }
    test(arr);
       

猜你喜欢

转载自itnotesblog.iteye.com/blog/2411091