美文网首页
函数新特性

函数新特性

作者: 幸宇 | 来源:发表于2020-09-05 15:17 被阅读0次

传多个参数的函数
1、Rest and Spread 操作符:
用来声明任意数量的方法参数

function func(...args) {
    args.forEach(function (arg) {
        console.log(arg)
    })
}

func(1, 2, 3)
func(4,5,6,7,8,99)
image.png

2、generator 函数
控制函数的执行过程,手工暂停和恢复代码执行

function*doSomething(){
console.log("start");
 yield;  //遇到yield就会停下
 console.log("finshed")
}

var func1 = doSomething();
func1.next();
func1.next()
function* getStockPrice(stock) {
    while (true) {  //generator函数下面调用执行的时候并没有无限循环,只有当其值小于15的时候就会停止
        yield Math.random()*100
    }
}

var priceGenerator = getStockPrice("IBM");
var limprice = 15;
var price = 100;
while (price > limprice) {
    price = priceGenerator.next().value;
    console.log(`the generator return ${price}`)
}
console.log(`buying at ${price}`)

3、destructuring析构表达式
通过表达式将对象或数组拆解成任意数量的变量
从对象拆:

function getStock() {
    return {
        code: "ibm",
        price: 100,
        obj: {
            price1: 200,
            price2:400
        }
    }
}

// var stock = getStock();
// var code = stock.code;
// var price = stock.price;

var { code, price } = getStock() //对象名要保持一致
var { code: codex, price } = getStock(); //起别名可以
console.log(codex, price)

var { code, obj: { price2 } } = getStock();// 可以拿到obj里的price2属性

从数组拆:

var arry1 = [1, 2, 3, 4];
var [number1, number2,...othes] = arry1;

console.log(number1, number2,othes)

var arry1 = [1, 2, 3, 4];
var [number1, number2,...othes] = arry1;

// console.log(number1, number2,othes)

function doSomething([number1, number2, ...othes]) {
    console.log(number1)
    console.log(number2)
    console.log(othes)
}

doSomething(arry1)

相关文章

  • 【一起来烧脑】一步学会TypeScript入门

    字符串新特性变量和参数新特性函数新特性匿名函数for of循环TypeScript语言中的面向对象特性 理解ES5...

  • 函数新特性

    ES6函数作为对象方法的简写方式:

  • 函数新特性

    传多个参数的函数1、Rest and Spread 操作符:用来声明任意数量的方法参数 2、generator 函...

  • ES6在企业中的应用

    模板字符串 解构赋值解构赋值最常用的在于函数。 数组spread es6浅拷贝 函数新特性之箭头函数 函数新特性之...

  • MySQL8.0新特性-窗口函数

    MySQL8.0新特性-窗口函数

  • TS 函数新特性

    # 1:rest and spread操作符 --- ...args No 1 : 用来声明任意数量的方法参数 f...

  • ES6,ES7,ES8,ES9,ES10新特性

    ES6新特性 一些常用新特性 类 模块化 箭头函数 函数参数默认值 模板字符串 解构赋值 延展操作符 对象属性简写...

  • Kotlin —— 扩展函数

    一、前言 Kotlin中的扩展函数特性让我们的代码变得更加简单和整洁。扩展函数是Kotlin语言中独有的新特性,利...

  • 新特性 函数式接口

    概述 有且只有一个抽象方法的接口,称之为函数式接口,当然接口中可以包含其它的方法(默认,静态,私有) 定义一个函数...

  • @FunctionalInterface函数式接口

    JDK8新特性:函数式接口@FunctionalInterface的使用说明

网友评论

      本文标题:函数新特性

      本文链接:https://www.haomeiwen.com/subject/mgqfektx.html