面试总结四

面试总结四

var mod, b;

        (function test(a) {
            "use strict";
            a = a || {};
            var arr = [], count = 0, table = {};

            a.set = function (key, value, callback) {
                arr.push (value);
                table[key] = value;
                count += value;
                if (callback) {
                    callback();
                };
            };

            a.urlParse = function (url) {
                if (url.indexOf("?") >= 0) return;
                var str = url.split("?")[1].split(/&|=/), result = {};

                for (var i = 0, len = str.length; i < len; i += 2) {
                    table[str[i]] = str[i+1] - 0;
                    result[str[i]] = str[i+1] - 0;
                    count += table[str[i]];
                }
                return result;
            };

            a.get = function(key) {
                return table[key] ? table[key]: "Don't has the value of " + key;
            };

            a.count =function() {
                return count;
            };

            b = a;

        })(mod);

        // 题1:下面输出结果是什么?
        console.log(mod); // => undefined

        b.set ("dog", 200); 
        // 题2:下面输出结果是什么?
        console.log(b.get("dog")); // => 200

        // 定时器调用会在最后输出。
        setTimeout(function() {
            // 题3:下面输出结果是什么?
            console.log(b.get("tiger")); // => Don't has the value of tiger
            b.set("cat", 400, function() {
                //题4:下面输出结果是什么?
                console.log(b.get("cat")); // => 400
            })

        }, 0);

        // 题5:下面输出结果是什么?
        console.log(b.get("cat")); // => Don't has the value of cat

        //题6:下面输出结果是什么?
        console.log(b.count()); // => 200

        //题7:下面输出结果是什么?
        // => undefined
        console.log(b.urlParse("http://www.abc.com?dog=500&pig=200&tiger=50&cat=600"));

        //题8:下面输出结果是什么?
        console.log(b.count()); // => 200

image

js  面试