2016-05-09 5 views
0

Ich habe eine ES6-Klasse, die ein Config-Objekt erfordert. Wenn eine Eigenschaft fehlt, möchte ich einen Fehler ausgeben.Throw-Fehler, wenn das Konfigurationsobjekt nicht alle erforderlichen Eigenschaften enthält

Die Lösung, wenn Sie den Code kurz halten gefunden und keinen if(!object.propN)... für jede Eigenschaft hinzufügen zu tun:

class myClass { 
    constructor(config) { 
    if (!config) { 
     throw new Error("You must provide a config object"); 
    } 

    this.prop1 = config.prop1; 
    this.prop2 = config.prop2; 
    this.prop3 = config.prop3; 
    this.prop4 = config.prop4; 

    for (const key in this) { 
     if (!this[key]) { 
     throw new Error(`Config object miss the property ${key}`); 
     } 
    } 

    } 
} 

Ist es OK, dies in Javascript zu tun?

+0

macht dies 'if (! This [key]) {' bedeutet, dass keine der 'config' Eigenschaften leer sein darf (alle Requisiten sollten gefüllt sein)? – RomanPerekhrest

+0

Ja, alle Eigenschaften sollten nicht leere Zeichenfolgen sein – IggY

Antwort

1

Für configs, wir in der Regel die Funktion verwenden Destructuring assignment zu prüfen, ob die Eigenschaften in einem Objekt sind oder nicht durch den Zucker von ES6 mit (ES2015).

Außerdem können wir mit dieser Funktion auch Standardwerte für die Konfigurationen festlegen.

{prop1, prop2, prop3: path='', prop4='helloworld', ...others} = config 

Nach Destrukturierung Zuordnung erfolgt ist, müssen nur vor einem Scheck tun, was wir mit der spezifischen Konfiguration tun werden. dh

if (!prop1) { 
    throw new Error(`Config object miss the property ${prop1}`); 
} 
doSomething(prop1); 

Aber wenn Sie noch alle configs am Anfang überprüfen möchten, können Sie so etwas wie dieses,

class myClass { 
    constructor(config) { 
    if (!config) { 
     throw new Error("You must provide a config object"); 
    } 

    // You can assign the values to a new object like "tmpConfig" or just assign them back to "config" for the case config.propX === undefined 
    ({prop1: config.prop1=undefined, prop2: config.prop2=undefined, prop3: config.prop3=undefined, prop4: config.prop4=undefined} = config); 

    for (const key in config) { 
     if (!config[key]) { 
     throw new Error(`Config object miss the property ${key}`); 
     } 
    } 

    Object.assign(this, config); 
    } 
} 

Oder nur mit Object.assign(), indem Standardwerte tun,

class myClass { 
    constructor(config) { 
    if (!config) { 
     throw new Error("You must provide a config object"); 
    } 

    let requiredConfigs = { 
     prop1: undefined, 
     prop2: undefined, 
     prop3: undefined, 
     prop4: undefined, 
    } 
    let tmpConfig = Object.assign(requiredConfigs, config); 

    for (const key in tmpConfig) { 
     if (!tmpConfig[key]) { 
     throw new Error(`Config object miss the property ${key}`); 
     } 
    } 

    Object.assign(this, tmpConfig); 
    } 
} 

=>Wir verwenden Destructionation-Zuweisung und Object.assign() viel für die Einstellung von Einstellungen.

0

Um eine vorzeitige und übermäßige Einstellung aller config Eigenschaften in this Objekt zu vermeiden, sollten Sie die Prüfung für eine leere Eigenschaft Auftreten zuvor platzieren.
Die Lösung mit Array.some Funktion:

class myClass { 
    constructor(config) { 
    if (!config || typeof config !== "object") { 
     throw new Error("You must provide a config object!"); 
    } else if (Object.keys(config).some((k) => !config[k])) { // returns 'true' if any empty property is found 
     throw new Error(`Config object has empty property(ies)!`); 
    } 

    this.prop1 = config.prop1; 
    this.prop2 = config.prop2; 
    this.prop3 = config.prop3; 
    this.prop4 = config.prop4; 

    } 
} 
Verwandte Themen