【问题标题】:module arguments override function arguments模块参数覆盖函数参数
【发布时间】:2017-10-16 11:25:22
【问题描述】:

我从module.exports 引用我的生成器方法,但它得到arguments 数组module 不是被调用的函数。

const Promise = require('bluebird');

const inc = Promise.coroutine(function* (model, condition, fields, options = {}) {});

module.exports = {
    inc: (model, condition, fields, options = {}) => { 
        //reveiving all the arguments fine
        return inc.apply(null, arguments);  //but "arguments" array contains the values of "module", not the function
    }
};

arguments数组:

0 - require function
1 - Module
2 - file path
3 - directory path

【问题讨论】:

    标签: javascript node.js ecmascript-6 apply


    【解决方案1】:

    Arrow functions do not bind an arguments object.

    应该是:

    module.exports = {
        inc: (...args) => inc(...args)
    };
    

    除非直接导出会导致函数上下文出现问题,否则可以只是:

    module.exports = {
        inc
    };
    

    【讨论】:

    • 我得到了它的工作,但我不明白这个问题。你能解释一下你的第一行箭头函数不绑定参数对象吗?
    • arguments 不能在箭头中使用来指代它们的参数,arguments 指的是父函数范围参数(在你的情况下是模块函数) - 类似于箭头中的this。链接的参考说明了这一点。
    • 我知道arguments 属于父函数范围(在链接中也有说明) 但我不明白箭头函数不能/不能'没有arguments。如果箭头函数是父函数怎么办?我刚刚检查了将箭头函数设为父函数并收到了arguments 对象..
    • 那根本就不会有arguments。因为箭头在设计上没有自己的argumentsthisarguments 指的是从非箭头函数(模块函数)接收到的那个。如果没有这样的功能,即当一个脚本在浏览器中被评估为
    • 是的,你是对的。我在 nodejs 中测试它,它的行为不同。在浏览器中进行测试证明了您所说的。
    猜你喜欢
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2016-11-26
    • 2017-07-12
    • 2016-10-01
    • 2013-02-02
    相关资源
    最近更新 更多