简体   繁体   English

使用BabelJs进行编译时如何忽略代码块?

[英]How to ignore a block of code when transpiling with BabelJs?

I'm trying to reference this in ES2015, with Babeljs. 我想引用this在ES2015,与Babeljs。 This problem I am having is Babeljs keeps moving this out of the scope into the external scope. 我遇到的问题是Babeljs不断将this从范围移到外部范围。 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. 我希望Babeljs有一个注释,或者某种形式的块符号,我可以用它来忽略代码,以免被编译。

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') ). 如果那里有猫鼬大师,那么也许还有另一种访问相关属性和函数的方法( this.isNew || this.isModified('email') )。

Here is the ES2015 code. 这是ES2015代码。

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');
}

In the if-statement if (this.isNew || this.isModified('email')) the pre-transpiled code has references to this . 在if语句if (this.isNew || this.isModified('email')) ,预编译的代码具有this引用。 The scope of validate() is important because while in this scope I have access to Mongoosejs's Document API. validate()的范围很重要,因为在此范围内,我可以访问Mongoosejs的Document API。 Once the code moves out of the validate() scope I no longer have access to the Document API. 一旦代码移出validate()范围,我将不再有权访问Document API。

Transpiled code. 转译的代码。

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');
    }
}

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. 在此代码中,您会注意到if语句引用了超出validate函数范围之外的变量( if (_this.isNew || _this.isModified('email')) )。 Because of this (no pun intended) move, I am losing access to Mongoosejs's Document API. 由于此举(没有双关语),我无法访问Mongoosejs的Document API。

Any suggestions will be greatly appreciated. 任何建议将不胜感激。

Don't use arrow function , just use the function keyword: 不要使用箭头功能 ,只需使用function关键字:

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

An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value . 箭头函数表达式(也称为胖箭头函数)的语法比函数表达式短,并且在词法上绑定了this值

Emphasis mine. 强调我的。 This means that this inside the arrow function will be this in the same lexical context outside the arrow function. 这意味着, this箭头函数内将this在箭头功能之外的相同的词汇上下文。 This is intentional and different from function syntax. 这是有意的,与function语法不同。

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

相关问题 使用BabelJS进行转译时如何保持AngularJS文件的顺序 - How do you keep the order of AngularJS files when transpiling with BabelJS 使用Webpack(BabelJS)进行编译后,React应用无法呈现 - React app not rendering after transpiling with webpack (BabelJS) part 2 如何让 Prettier 忽略一段代码? - How to make Prettier to ignore a block of code? 如何 append 到 babeljs 中的 div - How to append to the div in babeljs 如何使用gulp-babel转换nodejs Gulp-Task并忽略“导入”? - How transpiling nodejs Gulp-Task with gulp-babel and ignore “import”? 使用Webpack 4时BabelJS处理错误的文件 - BabelJS is processing the wrong files when working with Webpack 4 BabelJS如何实现尾部递归? - How does BabelJS achieve tail recursion? 如何在不转译代码的情况下在 Visual Studio Code 中使用 ES6 模块调试 Node.JS? - How to debug Node.JS with ES6 modules in Visual Studio Code without transpiling the code? 如何使用css,javascript或jquery忽略html“代码”块中的前导和尾随换行符? - How can I ignore leading and trailing linebreaks within an html “code” block using css, javascript or jquery? 我们如何在现代Web浏览器中使用es6模块而不转换代码 - How can we use es6 modules in modern web browsers without transpiling the code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM