Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript es6类中的属性验证_Javascript_Constructor_Ecmascript 6_Setter_Es6 Class - Fatal编程技术网

Javascript es6类中的属性验证

Javascript es6类中的属性验证,javascript,constructor,ecmascript-6,setter,es6-class,Javascript,Constructor,Ecmascript 6,Setter,Es6 Class,在类中设置属性之前,我需要验证属性。我为类中的每个属性定义了setter方法,如下所示: class SetterDemo { constructor(document) { this.document = { _type: 'SetterDemo' }; this.document.prop1 = document.prop1; } get prop1() { return this

在类中设置属性之前,我需要验证属性。我为类中的每个属性定义了setter方法,如下所示:

class SetterDemo {
    constructor(document) {
        this.document = {
            _type: 'SetterDemo'
        };
        this.document.prop1 = document.prop1;
    }
    get prop1() {
        return this.document.prop1;
    }
    set prop1(value) {
        //validating data. This is just an example. My validations will be a lot complex
        if (!value) {
            throw 'Invalid data';
        }
        this.document.prop1 = value;
    }
}
只要我这样做,它就可以正常工作:

let instance1 = new SetterDemo({prop1: 'abc'});
instance1.prop1 = null; //Throws error. Good. Just like I want it to.
但当我这么做的时候:

let instance2 = new SetterDemo({prop1: null});
它正在创建一个实例而不抛出错误。我希望它抛出一个错误,因为“prop1”无效。似乎prop1的setter方法没有在构造函数中被调用。有没有办法在构造函数中使用setter方法?或者有没有一种方法可以在我的setter方法和构造函数中使用公共验证器函数

注意:我尝试在类之外创建一些验证器函数,并在setter和构造函数中使用它们。这是我想要的,但似乎不是正确的方式。下面是它的代码:

class SetterDemo {
    constructor(document) {
        this.document = {
            _type: 'SetterDemo'
        };
        if (!validateProp1(document.prop1)) {
            throw 'Invalid data';
        }

        this.document.prop1 = document.prop1;
    }
    get prop1() {
        return this.document.prop1;
    }
    set prop1(value) {
        if (!validateProp1(value)) {
            throw 'Invalid data';
        }
        this.document.prop1 = value;
    }
}

module.exports = SetterDemo;

// -- Private Functions -- //
//validator function for prop1
function validateProp1(value) {
    if (!value) {
        return false;
    } else {
        return true;
    }
}

问题是这条线

this.document.prop1 = document.prop1;
相反,它需要改变

this.prop1 = document.prop1;

发生的情况是您绕过了构造函数中的setter。

否则如果(value.prop1==null){throw“Err.”;}
我想您需要添加这一行。这一行是问题所在:
this.document.prop1=document.prop1
需要
this.prop1=document.prop1。FWIW外部验证完全正确,只是保持一致。您的错误是您绕过了构造函数中的setter。@AluanHaddad,您的评论正是我想要的。我曾想过,但我不确定,并正在试验它,现在它的工作预期。非常感谢。请将您的评论作为答案发布,以便我可以接受它作为正确的答案。