【问题标题】:Pre-condition checking in typescript打字稿中的前置条件检查
【发布时间】:2020-03-03 16:19:06
【问题描述】:

我有一个typescript 类,其中有检查变量类型的方法。 我如何确定doProcess()开头的那个方法用于处理下面的内容?

class MyClass {

public static arr : any[] = [];

// main method
public static doProcess(object : object , justForObjects : boolean = false){
    if (justForObjects){
        // here should specify which checking method use in *** line
    } else {

    }

    for (let key in object){
        if (object.hasOwnProperty(key)){
            if (/* *** */){
                MyClass.doProcess(object[key] , justObjects)
            } else {
                MyClass.arr.push(object[key])
            }
        }
    }

return MyClass.arr;

}

 // check variables type methods
 private static is_object(value : any){
     return !!(typeof value === 'object' && !(value instanceof Array) && value);
 }
 private static is_object_or_array(value : any){
     return !!(typeof value === 'object' && value);
 }

}

let object = {
 'main color' : 'black',
 'other colors' : {
     'front color' : 'purple',
     'back color' : 'yellow',
     'inside colors' : {
         'top color' : 'red',
         'bottom color' : 'green'
     }
 }
}

MyClass.doProcess(object , true);

我知道它可以在同一个 for 循环中完成(如下所示),但我想先找到一种方法。

    for (let key in object){
        if(object.hasOwnProperty(key)){
            if (justForObjects){
                if (MyClass.is_object(object[key])){
                    // do something
                }
            } else {
                if (MyClass.is_object_or_array(object[key])){
                    // do something else
                }
            }
        }

    }

感谢您的提示

【问题讨论】:

    标签: javascript typescript typescript-class


    【解决方案1】:

    函数可以简单地赋值给变量,即:

    public static doProcess(obj : object , justForObjects : boolean = false) {
    
        var check_fn = justForObjects ? MyClass.is_object : MyClass.is_object_or_array;
    
        for (let key in obj) {
            if (check_fn(obj[key])) ...
        }
    
    }
    
     // check variables type methods
     private static is_object(value : any) : boolean { ... }
     private static is_object_or_array(value : any) : boolean { ... }
    

    【讨论】:

    • 还要注意,最好在其构造函数中配置类(即var c = new MyClass(true); // just for objects,然后有一个干净的接口来处理实际项目(c.doProcess(obj); // only one parameter)。
    • 感谢您的回答。您认为可以使用 bind 代替变量吗?如果你同意,你怎么做才对?
    • @ali.rezaie:我不确定bind 会在这里做什么,你能举个例子吗?这是一个简单问题的简单方法,但正如我在上面所写的,如果您发布带有实际问题的实际代码(很难猜出MyClass.doProcess 的含义),可能会有更好的方法。我的建议是创建一个完整的工作示例并将其发布在 codereview.stackexchange.com 上。您的颜色示例表明它应该像其他答案中提到的那样进行递归处理,但是如果不知道它应该做什么就不可能说出来。
    • 我编辑了我的问题并完成了 doProcess() 方法。如您所见,此方法返回数组中对象的所有值,它还包括嵌套值
    【解决方案2】:

    看看递归方法是否对你有帮助:

    interface CustomObject {
      [path: string]: ObjectValue;
    }
    
    type ObjectValue = string | CustomObject | CustomObject[];
    
    class MyClass {
      /**
       *
       *
       * @recursive
       */
      public static doProcess(
        object: CustomObject,
        justForObjects: boolean = false
      ) {
        for (const key in object) {
          const value = object[key];
    
          if (this.isObjectOrArray(value, justForObjects)) {
            this.doProcess(value as CustomObject, justForObjects);
            continue;
          } else {
            // do something;
            continue;
          }
        }
      }
      /**
       *
       *
       * Check is object or array
       */
      private static isObjectOrArray(
        value: ObjectValue,
        justForObjects: boolean = false
      ) {
        // objects and arrays
        if (!justForObjects) return !!(typeof value === "object" && value);
    
        if (Array.isArray(value)) throw new TypeError("Array value not allowed");
    
        // only objects
        return !!(typeof value === "object" && !(value instanceof Array) && value);
      }
    }
    
    let object = {
      "main color": "black",
      "other colors": {
        "front color": "purple",
        "back color": "yellow",
        "inside colors": {
          "top color": "red",
          "bottom color": "green"
        }
      }
    } as CustomObject;
    
    MyClass.doProcess(object, true);
    
    

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 2018-11-03
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多