什么是JavaScript中的yield*关键字/表达式?

184 阅读2分钟

Yield是一个关键字/表达式,用于停止生成器函数的执行。生成器函数与其他函数类似,但它们的不同之处在于,生成器函数中返回的值是yield关键字。嵌套的函数或回调不能允许屈服表达。 在屈服表达式返回的对象中可以观察到两个特征,一个是值,一个是完成,分别是实际值和布尔值。 当生成器函数完全完成时,那么布尔值就会返回真,反之亦然。

如果屈服表达式被暂停,那么它也会暂停生成器函数,只有当下一个方法被调用时,它才会重新启动,直到另一个返回表达式。屈服表达式/关键字的语法如下。

function* name(arguments) {statements}

其中name 代表函数的名称,arguments 是为函数传递的参数,statements 代表函数的主体。

以下是yield*表达式/关键词的特点。

  • 节省内存
  • 懒惰的评估
  • 控制流是异步的

现在我们将举一个例子,通过这个例子你可以很容易地理解如何在JavaScript中使用yield*关键字/表达式。

function* showNum(x) {

    while (x > 0) {

        yield x--;

    }

}

 
//instance is created for function showNum

const generator_val = showNum(4);

 
//return 4 as 4 is passed to the function showNum yield expression

console.log(generator_val.next().value);

// return 3

console.log(generator_val.next().value);

//return 2

console.log(generator_val.next().value);

//return 1

console.log(generator_val.next().value);

输出

另一方面,yield*是一个关键字/表达式,可以用来表示一个迭代对象或其他生成器函数。yield* 迭代并相应地返回值,直到布尔值为真。yield*表达式/关键字的语法如下。

yield* expression

现在,我们将介绍一个屈服*表达式/关键字的例子。

<html>

<head>

    <title>JavaScript yield* keyword/expression</title>

</head>

<body>

    <script>

        function* first_func() {

            yield 10;

        }

        function* second_func() {

            yield* first_func();

        }

        const generator_iterator = second_func();

        console.log(generator_iterator.next().value);

 
    </script>

</body>

</html>

输出

例子

在这个例子中,generatorfunc1()函数通过next()函数返回的值与通过generatorfunc2()函数获得的值相似。随后,通过这个generatorfunc2()函数,我们可以很容易地插入更多的生成器,只要我们能做到。

<html>

<head>

    <title>JavaScript yield* representing other generator </title>

</head>

<body>

    <script>

        function* generatorfunc1() {

            yield 22;

            yield 33;

            yield 44;

        }

        function* generatorfunc2() {

            yield 11;

            yield* generatorfunc1();

            yield 55;

        }

        const iterative_value = generatorfunc2();

                                // it return value 11 whereas done i.e. boolean value is false

        console.log(iterative_value.next());

                                // it return value 22 whereas done i.e. boolean value is false

        console.log(iterative_value.next());

                                // it return value 33 whereas done i.e. boolean value is false

        console.log(iterative_value.next());

                                // it return value 44 whereas done i.e. boolean value is false

        console.log(iterative_value.next());

                                // it return value 55 whereas done i.e. boolean value is false

        console.log(iterative_value.next());

                                // it return undefined value whereas done i.e. boolean value is true

        console.log(iterative_value.next());

    </script>

</body>

</html>

输出

总结

读完这篇文章后,你已经熟悉了yield*关键字/表达式。如果你使用了yield*表达式,你就不会面临回调问题。yield*表达式背后的概念是,函数可以自愿恢复或停止,直到它获得它所需要的东西。我们还列举了一些例子,帮助你了解在JavaScript中更好地使用yield*表达式/关键字。