继续Mootools常用方法扩展,这次是Window类上的扩展,也就是全局函数。

方法:$params

说明:获取页面URL参数。你可以使用任何指定的分隔符,比如’#’,默认为’#’。

Window.implement({
    $params: function(delimiter) {
        ///<summary>
        /// 获取页面URL的参数,返回包含参数的hash对象
        ///</summary>
        ///<param name="delimiter" type="String">参数和URL的分隔符,比如?或者#,默认为#</param>
        ///<returns type="Hash" />
        delimiter = (delimiter || '#').escapeRegExp();
        var params = new Hash();
        try {
            var requestStr = (location.toString().match(new RegExp(delimiter + '(.+)$')) || [0, 0])[1];
            if (requestStr) {
                params = requestStr.toHash(true);
            }
        }
        catch (e) {
        }
        return params;
    }
}

方法:$dump

说明:类似于PHP中的var_dump方法,打印指定的变量或者对象的内容,返回描述变量内容的字符串。

Window.implement({
    $dump: function(obj, options) {
        ///<summary>
        /// 打印一个变量或者对象的内容,返回描述变量内容的字符串
        ///</summary>
        ///<param name="obj" type="Object">要打印的变量</param>
        ///<param name="options" type="Object">
        /// 选项:
        ///   delimiter: 变量描述之间的分隔符,默认为“, ”;
        ///   showFunction: 是否打印函数内容,默认为false;
        ///   showClass: 是否打印类的构造函数,默认为false;
        ///   showArrayValue: 是否打印数组内容,默认为true;
        ///   sort: 排序方式,默认不排序,可以为asc(顺序), desc(逆序)
        ///</param>
        ///<returns type="Hash" />
        options = options || {};
        var dlm = options.delimiter || ', ';
        var rf = options.showFunction ? true : false;
        var rc = options.showClass ? true : false;
        var rav = options.showArrayValue === false ? false : true;
        var st = options.sort ? options.sort : null;
        var s = [];
        switch ($type(obj)) {
            case 'element':
            case 'hash':
            case 'native':
            case 'object':
            case 'window':
                var v, ty;
                for (var p in obj) {
                    try {
                        v = obj[p];
                        ty = $type(v);

                        if ('function' == ty && !rf) {
                            s.push(['[', p, '](', ty, ') => [function Function]'].join(''));
                        }
                        else if (('class' == ty || 'native' == ty) && !rc) {
                            s.push(['[', p, '](', ty, ') => [class Class]'].join(''));
                        }
                        else if ('string' == ty) {
                            s.push(['[', p, '](', ty, ' ', v.length, ') => ', v].join(''));
                        }
                        else if ('array' == ty) {
                            if (rav && rf) {
                                s.push(['[', p, '](', ty, ' ', v.length, ') => ', v].join(''));
                            }
                            else if (rav && !rf) {
                                var tv, tty, ta = [], len = v.length;
                                for (var i = 0; i < len; i++) {
                                    if ('function' == $type(v[i])) {
                                        ta.push('[function Function]');
                                    }
                                    else {
                                        ta.push(v[i]);
                                    }
                                }
                                s.push(['[', p, '](', ty, ' ', v.length, ') => ', ta].join(''));
                            }
                            else {
                                s.push(['[', p, '](', ty, ' ', v.length, ') => [array Array]'].join(''));
                            }
                        }
                        else {
                            s.push(['[', p, '](', ty, ') => ', v].join(''));
                        }
                    }
                    catch (e) {
                        ty = 'unknow';
                        v = '[Permission Denied]';
                        s.push(['[', p, '](', ty, ') => ', v].join(''));
                    }
                }
                break;
            case 'array':
                var l = obj.length;
                var v, t;
                for (var i = 0; i < l; i++) {
                    v = obj[i];
                    t = $type(v);
                    if ('function' == t && !rf) {
                        s.push(['[', i, '](', ty, ') => [function Function]'].join(''));
                    }
                    else {
                        s.push(['[', i, '](', ty, ') => ', v].join(''));
                    }
                }
                break;
            case 'string':
                s.push([$type(obj), ' ', obj.length, ' => ', obj].join(''));
                break;
            default:
                s.push([$type(obj), ' => ', obj].join(''));
                break;
        }

        if (st) {
            return s.sortBy({ type: st }).join(dlm);
        }

        return s.join(dlm);
    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.