如何在使用BabelJs的时候忽略代码块? [英] How to ignore a block of code when transpiling with BabelJs?

查看:327
本文介绍了如何在使用BabelJs的时候忽略代码块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ES2015中引用这个与Babeljs。我遇到的这个问题是Babeljs继续将这个移出范围进入外部范围。这打破了代码。我希望Babeljs有一个评论,或某种阻止符号,我可以使用它忽略代码,所以它不会被淹没。

I'm trying to reference this in ES2015, with Babeljs. This problem I am having is Babeljs keeps moving this out of the scope into the external scope. This breaks the code. I am hoping Babeljs has a comment, or some sort of block notation I can use to have it ignore the code so it won't be transpiled.

如果有任何Mongoose大师在那里,也许还有另一种方式访问​​有问题的属性和功能( this.isNew || this .isModified('email'))。

If there are any Mongoose guru's out there, maybe there is another way to access the property and function in question (this.isNew || this.isModified('email')).

这是ES2015代码。

Here is the ES2015 code.

setDuplicateEmailValidation(){
    this.schema.path('email').validate((email, fn) => {
        let User = mongoose.model('User');

        // Check only when it is a new user or when email field is modified
        if (this.isNew || this.isModified('email')) {
            User.find({ email: email }).exec((err, users) => {
                fn(!err && users.length === 0);
            });
        } else fn(true);
    }, 'Email already exists');
}

在if语句 if(this.isNew || this.isModified('email'))预先转贴的代码引用了这个 validate()的范围非常重要,因为在此范围内,我可以访问 Mongoosejs的文档 API。一旦代码移出 validate()范围,我就不再能访问Document API。

In the if-statement if (this.isNew || this.isModified('email')) the pre-transpiled code has references to this. The scope of validate() is important because while in this scope I have access to Mongoosejs's Document API. Once the code moves out of the validate() scope I no longer have access to the Document API.

代码

function setDuplicateEmailValidation() {
            var _this = this;
        this.schema.path('email').validate(function (email, fn) {
            var User = _mongoose2['default'].model('User');

            // Check only when it is a new user or when email field is modified
            if (_this.isNew || _this.isModified('email')) {
                User.find({ email: email }).exec(function (err, users) {
                    fn(!err && users.length === 0);
                });
            } else fn(true);
        }, 'Email already exists');
    }
}

在这段代码中,你会注意到if -statement引用超出验证函数范围的变量( if(_this.isNew || _this.isModified('email')))。因为这个(没有双关意图)移动,我正在失去访问Mongoosejs的文档API。

In this code, you'll notice that the if-statement references a variable (if (_this.isNew || _this.isModified('email'))) that is outside the scope of the validate function. Because of this (no pun intended) move, I am losing access to Mongoosejs's Document API.

任何建议将不胜感激。

推荐答案

不要使用箭头功能,只需使用函数关键字:

Don't use arrow function, just use the function keyword:

this.schema.path('email').validate(function (email, fn) {




与函数表达式相比,箭头函数表达式(也称为胖箭头函数)具有较短的语法,并且词法地绑定此值

强调我的,这意味着arrow函数内的这个将是这个在箭头函数之外的同一个词汇语境中,这是有意的,不同于函数语法。

Emphasis mine. This means that thisinside the arrow function will be this in the same lexical context outside the arrow function. This is intentional and different from function syntax.

这篇关于如何在使用BabelJs的时候忽略代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆