JavaScript文章中的奇怪语法

时间:2016-12-22 23:08:53

标签: javascript node.js

在这里查看这篇文章:

http://www.2ality.com/2015/03/no-promises.html#comment-3064041816

作者Axel博士,引用他的示例库,如下所示(逐字):

  /**
     * Run the generator object `genObj`,
     * report results via the callbacks in `callbacks`.
     */
    function runGenObj(genObj, callbacks = undefined) {
        handleOneNext();

        /**
         * Handle one invocation of `next()`:
         * If there was a `prevResult`, it becomes the parameter.
         * What `next()` returns is what we have to run next.
         * The `success` callback triggers another round,
         * with the result assigned to `prevResult`.
         */
        function handleOneNext(prevResult = null) {
            try {
                let yielded = genObj.next(prevResult); // may throw
                if (yielded.done) {
                    if (yielded.value !== undefined) {
                        // Something was explicitly returned:
                        // Report the value as a result to the caller
                        callbacks.success(yielded.value);
                    }
                } else {
                    setTimeout(runYieldedValue, 0, yielded.value);
                }
            }
            // Catch unforeseen errors in genObj
            catch (error) {
                if (callbacks) {
                    callbacks.failure(error);
                } else {
                    throw error;
                }
            }
        }
        function runYieldedValue(yieldedValue) {
            if (yieldedValue === undefined) {
                // If code yields `undefined`, it wants callbacks
                handleOneNext(callbacks);
            } else if (Array.isArray(yieldedValue)) {
                runInParallel(yieldedValue);
            } else {
                // Yielded value is a generator object
                runGenObj(yieldedValue, {
                    success(result) {
                        handleOneNext(result);
                    },
                    failure(err) {
                        genObj.throw(err);
                    },
                });
            }
        }

        function runInParallel(genObjs) {
            let resultArray = new Array(genObjs.length);
            let resultCountdown = genObjs.length;
            for (let [i,genObj] of genObjs.entries()) {
                runGenObj(genObj, {
                    success(result) {
                        resultArray[i] = result;
                        resultCountdown--;
                        if (resultCountdown <= 0) {
                            handleOneNext(resultArray);
                        }
                    },
                    failure(err) {
                        genObj.throw(err);
                    },
                });
            }
        }
    }

    function run(genFunc) {
        runGenObj(genFunc());
    }

这个语法在我的机器上检查并且运行得很好,但我无法弄清楚上面这两段代码是如何“运行”的:

展示A:

               //
               runGenObj(yieldedValue, { // this is not a function, is it an object?
                success(result) {
                    handleOneNext(result);
                },
                failure(err) {
                    genObj.throw(err);
                },
            });
              //

展示b:

          //
    runGenObj(genObj, {    // same thing here (?)
        success(result) {
            resultArray[i] = result;
            resultCountdown--;
            if (resultCountdown <= 0) {
                handleOneNext(resultArray);
            }
        },
        failure(err) {
            genObj.throw(err);
        },
    });
      //

它一定是ES6的东西,任何人都能告诉我这是什么意思吗?

1 个答案:

答案 0 :(得分:3)

在对象文字中有一个用于声明事物的简写。

对象获取具有变量site

值的键site的示例
let site = "SC"
console.log({
  site
})

将goodbye扩展为键goodbye的示例以及使用指定参数命名为goodbye的函数的值。

let say = {
  goodbye() {
    console.log("Bye")
  },
  hello() {
    console.log("Hello")
  }
}

say.hello()