简体   繁体   English

ECMAScript6类的异常行为

[英]ECMAScript6 class strange behavior

I am writing an ES6 program and am getting very off behavior from the output in WebStorm. 我正在编写一个ES6程序,并从WebStorm的输出中获得了非常好的表现。 Can someone clear this up - I am using Babel to transpile. 有人可以解决吗-我正在使用Babel进行转送。

class DateLocationValidator{
    _appointments;
    constructor(appointments){
        this._appointments = appointments;
        console.log(this._appointments);
    }

    validate(appointmentViewModel)
    {
        console.log('validating');

        if(this._appointments==null || this._appointments.length==0) {
            console.log('appointments null');
            console.log(this._appointments);
            return {
                outcome:true,
                reason:'',
                conflictId:0
            };
        }else{

            console.log('appointments not null');
            var result = this._appointments.where(function(appointment)
            {
                console.log('searching');
                if(appointment.startDate==appointmentViewModel.startDate && appointment.startDate==appointmentViewModel.startDate){
                    console.log('found');
                    return true;
                }
            });

            if(result.length>0){
                return {
                        outcome:true,
                        reason:'There is already an appointment scheduled for this location on this date',
                        conflictId:result[0].id
                };
            }
        }
    }
}

Here is the test: 这是测试:

it("Fails when appointment clashes exactly",function(){
        try {
            let appointments = [new AppointmentViewModel(1, new Date(2015, 1, 1), new Date(2015, 1, 2))];
            let validator = new DateLocationValidator(appointments);
            let appointmentViewmodel = new AppointmentViewModel(1, new Date(2015, 1, 1), new Date(2015, 1, 2));
            let result = new validator.validate(appointmentViewmodel);
            expect(result.outcome).toBe(false);
        }
        catch(excep){
            console.log(excep)
            expect(true).toBe(false);
        }
    });

'appointments null' is always output to the console. “ appointments null”始终输出到控制台。 this is after the array has been correctly output in the constructor. 这是在构造函数中正确输出数组之后。

Also, when I try to call a function from validate, I get an undefined error. 另外,当我尝试从validate调用函数时,出现未定义的错误。

Can anyone help? 有人可以帮忙吗?

Cheers M 干杯

For some reason, you were calling validate as a constructor. 由于某些原因,您将validate称为构造函数。 Omit that new keyword! 忽略该new关键字!

let result =     validator.validate(appointmentViewmodel);
//           ^^^

In real ES6 (not transpiled), this would have thrown an exception, as functions that are declared using method syntax cannot be called with new (they have no [[construct]] slot). 在实际的ES6(未编译)中,这将引发异常,因为使用方法语法声明的函数无法使用new调用(它们没有[[construct]]插槽)。

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

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