首页 > 请教 js中嵌套3层for 如何优化掉呢。3层for用来遍历数组的

请教 js中嵌套3层for 如何优化掉呢。3层for用来遍历数组的

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>无标题文档</title>
</head>

<body>
    <script>
    var reg = {
        isnan: /^[\D]+$/
    }

    function interface(name, methods) {
        if (!(reg.isnan.test(name) && arguments.length <= 2 && methods.constructor === Array)) {
            throw "接口对象属性值不能<2 methods必须是数组类型  name必须是string类型"
        }
        for (var i = 0; i < methods.length; i++) {
            if (!reg.isnan.test(methods[i]) || typeof methods[i] !== "string") {
                throw "接口抽象方法名类型必须是string"
            }
        }
        this.name = name;
        this.method = methods;
    }

    function test() {
        this.s = function() {}
        this.w = function() {}
    }
    var test1 = new test();

    function test3() {
        this.s = function() {}
        this.w = function() {}
    }
    var test1 = new test();
    var test2 = new test3();
    var b = new interface("s", ["s", "w"]);
    var d = new interface("s", ["s", "w"]);

    interface.checkImplement = function(object) {
        if (arguments.length < 2 || object.constructor !== Array) {
            throw "您并没有给指定的2个属性参数赋值,因此并不做接口规范检查"
        }
        interface.objects = object;
        for (var i = 1; i < arguments.length; i++) {
            var interobj = arguments[i];
            if (interobj.constructor !== interface) {
                throw "这个接口对象属性值必须是object并且constroctur必须是接口 类"
            }

            for (var j = 0; j < interobj.method.length; j++) {
                var method = interobj.method[j];
                for (var k = 0; k < interface.objects.length; k++) {
                    if (!interface.objects[k][method] || typeof interface.objects[k][method] !== "function") {
                        throw "object类没有实现接口 抽象方法,或者值不是一个function"
                    }
                }
            }
        }
    }

    interface.checkImplement([test1, test2], b, d)
    </script>
</body>

</html>

给你稍稍改了下,减了一层循环,不过没有测试,你先看看

interface.checkImplement = function(object) {
    // if (arguments.length < 2 || object.constructor !== Array) {
    if (arguments.length < 2 || !Array.isArray(object)) {
        throw "您并没有给指定的2个属性参数赋值,因此并不做接口规范检查";
    }
    interface.objects = object;

    // var allInterfaceMethods = [].splice.call(arguments, 1).reduce(function(r, c) {
    //     return r.concat(c.method);
    // }, []);

    var allInterfaceMethods = [];
    for (var i = 1; i < arguments.length; i++) {
        // 连接数组,生成大数组
        allInterfaceMethods = allInterfaceMethods.concat(arguments[i].method);
    }
    // 这里可以给 allInterfaceMethods 去重,不去重也不影响后面的逻辑


    // interface.objects.forEach(function(obj) {
    //     allInterfaceMethods.forEach(function(method) {
    //         if (typeof obj[method] !== "function") {
    //             throw new Error("object类没有实现接口 抽象方法,或者值不是一个function");
    //         }
    //     });
    // });

    for (var i = 0; i < interface.objects.length; i++) {
        var interobj = interface.objects[i];
        // 上面那个忘了判断类型,这里加上
        if (interobj.constrcutor !== interface) {
            throw new Error("这个接口对象属性值必须是object并且constroctur必须是接口 类");
        }

        for (var j = 0; j < allInterfaceMethods.length; i++) {
            var method = allInterfaceMethods[j];
            if (typeof interobj[method] !== "function") {
                throw new Error("object类没有实现接口 抽象方法,或者值不是一个function");
            }
        }
    }
};

总的来说,就是先把所的接口的方法都找出来,放在一个大数组里,去重(不去重也无所谓)。然后再遍历每个对象看是否实现了这些方法,就这里减少了一层 for

判断类型的时候不需要先判是否存在,直接用 typeof 就可以了,因为对不存在的属性进行 typeof 去处会得到 "undefined",肯定不是 "function"

【热门文章】
【热门文章】