简体   繁体   English

如何在Node.JS中使用.bind?

[英]How Do I use .bind in Node.JS?

Still struggling to get my head around .bind . 仍在努力使.bind

If I have... 如果我有...

 dbAccess.runCommand('INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
                            req.getConnection,
                            email)()
                .then(setProjectPermission(req, res, 'insertId', projectId, permissionLevel));

and.... 和....

function setProjectPermission(req, res, arg, projectId, permissionLevel) {

  return function(result) {

    var id = result.rows[0][arg];

    ...

Can I use .bind so I don't have to use a function which returns a function? 我可以使用.bind这样就不必使用返回函数的函数了吗? I'm struggling to know how to make the anonymous closure. 我正在努力了解如何进行匿名关闭。 For example if it was EC6 I would do this (I think)... 例如,如果是EC6,我会这样做(我认为)...

 dbAccess.runCommand('INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
                            req.getConnection,
                            email)()
                .then(result => setProjectPermission(req, res, result.insertId, projectId, permissionLevel));

and.... 和....

function setProjectPermission(req, res, idArg, projectId, permissionLevel) {

    var id = idArg;

    ...

Is this possible to do without returning the function which is expecting 'result' ? 是否可以在不返回期望“结果”的函数的情况下执行此操作?

For example if it was EC6 I would do this (I think)... 例如,如果是EC6,我会这样做(我认为)...

("EC6" => "ES2015" aka "ES6") Yes, exactly, and all up-to-date versions of Node support arrow function syntax. (“ EC6” =>“ ES2015”或“ ES6”)是的,确切地说,所有最新版本的Node都支持箭头函数语法。 Except that your () at the end are a bit dodgy: 除了最后的()有点狡猾:

dbAccess.runCommand('INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
                        req.getConnection,
                        email)()
// ---------------------------^^ These shouldn't be there, you've *already* called `runCommand`
            .then(result => setProjectPermission(req, res, result.insertId, projectId, permissionLevel));

So (apologies, I've also adjusted indentation): 因此(抱歉,我也调整了缩进):

dbAccess.runCommand(
        'INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
        req.getConnection,
        email
    )
    .then(result => setProjectPermission(req, res, result.insertId, projectId, permissionLevel));

If you need to run on legacy versions of Node, you can use function syntax: 如果需要在旧版Node上运行,则可以使用function语法:

dbAccess.runCommand(
        'INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
        req.getConnection,
        email
    )
    .then(function result {
        return setProjectPermission(req, res, result.insertId, projectId, permissionLevel);
    });

Since you're not using this in there, no need for bind . 由于您不在此处使用this ,因此不需要bind

If you were using this in there, you'd add bind to the end: 如果您正在使用this在那里,你会添加bind到最后:

dbAccess.runCommand(
        'INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
        req.getConnection,
        email
    )
    .then(function result {
        return setProjectPermission(req, res, result.insertId, projectId, permissionLevel);
    }.bind(this)); // <===== `bind` is here

...or use var _this = this; ...或使用var _this = this; and use _this instead, or any of the tricks described in this question's answers . 并改用_this此问题答案中描述的任何技巧。 But with arrow functions, you don't have to worry about that, they close over this . 但是使用箭头功能,您不必担心,它们会关闭 this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM