= array(); } $post['classname'] = 'post'; } function comment_format(&$post) { global $conf, $uid, $gid, $forumlist; if (empty($post)) return; $forum = $post['fid'] ? forum_read($post['fid']) : ''; $thread = well_thread_read_cache($post['tid']); if ($thread) { //$post['fid'] = $thread['fid']; $post['closed'] = $thread['closed']; $post['subject'] = $thread['subject']; $post['url'] = $thread['url']; } else { $post['closed'] = 0; $post['subject'] = lang('thread_not_exists'); $post['url'] = ''; } $post['create_date_fmt'] = humandate($post['create_date']); //$post['message'] = stripslashes(htmlspecialchars_decode($post['message'])); $user = user_read_cache($post['uid']); $post['username'] = array_value($user, 'username'); $post['user_avatar_url'] = array_value($user, 'avatar_url'); $post['user'] = $user ? user_safe_info($user) : user_guest(); isset($post['floor']) || $post['floor'] = 0; // 权限判断 $post['allowupdate'] = 2 == array_value($forum, 'comment', 0) && ($uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowupdate')); $post['allowdelete'] = group_access($gid, 'allowuserdelete') && $uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowdelete'); $post['user_url'] = url('user-' . $post['uid'] . ($post['uid'] ? '' : '-' . $post['pid'])); if ($post['files'] > 0) { list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($post['pid']); // 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法 if (2 == $conf['attach_on']) { foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换成图床 $post['message'] = FALSE !== strpos($post['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $post['message']) : $post['message']; } } $post['filelist'] = $filelist; } else { $post['filelist'] = array(); } $post['classname'] = 'post'; } function comment_format_message(&$val) { global $conf; if (empty($val)) return; // 使用云储存 if (1 == $conf['attach_on'] && 1 == $val['attach_on']) { $val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']); } elseif (2 == $conf['attach_on'] && 2 == $val['attach_on']) { // 使用图床 list($attachlist, $imagelist, $filelist) = well_attach_find_by_tid($val['tid']); foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换成图床 $val['message'] = FALSE !== strpos($val['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $val['message']) : $val['message']; } } else { $val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']); } //$val['message'] = stripslashes(htmlspecialchars_decode($val['message'])); } // 把内容中使用了云储存的附件链接替换掉 function comment_message_replace_url($pid, $message) { global $conf; if (0 == $conf['attach_on']) { $message = FALSE !== strpos($message, '="../upload/') ? str_replace('="../upload/', '="upload/', $message) : $message; $message = FALSE !== strpos($message, '="/upload/') ? str_replace('="/upload/', '="upload/', $message) : $message; } elseif (1 == $conf['attach_on']) { // 使用云储存 $message = str_replace('="' . $conf['cloud_url'] . 'upload/', '="upload/', $message); } elseif (2 == $conf['attach_on']) { // 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法 list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($pid); foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换回相对链接 $message = $attach['image_url'] && FALSE !== strpos($message, $attach['image_url']) ? str_replace($attach['image_url'], $url, $message) : $message; } } return $message; } function comment_filter($val) { unset($val['userip']); return $val; } function comment_highlight_keyword($str, $k) { $r = str_ireplace($k, '' . $k . '', $str); return $r; } // //
function comment_message_format(&$s) { if (xn_strlen($s) < 100) return; $s = preg_replace('#.*?
#is', '', $s); $s = str_ireplace(array('
', '
', '
', '

', '', '', '', '' . ''), "\r\n", $s); $s = str_ireplace(array(' '), " ", $s); $s = strip_tags($s); $s = preg_replace('#[\r\n]+#', "\n", $s); $s = xn_substr(trim($s), 0, 100); $s = str_replace("\n", '
', $s); } // 对内容进行引用 function comment_quote($quotepid) { $quotepost = comment_read($quotepid); if (empty($quotepost)) return ''; $uid = $quotepost['uid']; $s = $quotepost['message']; $s = comment_brief($s, 100); $userhref = url('user-' . $uid); $user = user_read_cache($uid); $r = '
' . $user['username'] . ' ' . $s . '
'; return $r; } // 获取内容的简介 0: html, 1: txt; 2: markdown; 3: ubb function comment_brief($s, $len = 100) { $s = strip_tags($s); $s = htmlspecialchars($s); $more = xn_strlen($s) > $len ? ' ... ' : ''; $s = xn_substr($s, 0, $len) . $more; return $s; } ?>1-letter names for variables and functions in jQuery, JavaScript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

1-letter names for variables and functions in jQuery, JavaScript - Stack Overflow

programmeradmin28浏览0评论

I was looking at Twitter's static scripts and noticed that all variables and functions where just 1 character long, why and how do they do this? Has it something to do with performance? If so, why don't they give all elements on their website these kind of short names, maybe 2 characters long instead of 1 to avoid any collisions.

Example:

(function (A) {
A.fn.isScreenNameField = function () {
    return this.each(function () {
        var M = A(this);
        var F = A("#signup_username_url");
        var E = A("#screen_name_info");
        var D = A("#avail_screenname_check_indicator");
        var O;
        var C;
        var I;
        var N = M.val();
        var G = N;
        var H = N != "";
        var Q = /[a-zA-Z0-9_]/;

        function K() {
            if (H) {
                F.html(M.val())
            }
        }
        function L() {
            M.trigger("show-info");
            E.hide();
            D.show()
        }
        function B() {
            E.show();
            D.hide()
        }
        function P() {
            G = O;
            jQuery.ajax({
                type: "GET",
                url: "/users/username_available",
                data: {
                    username: O
                },
                dataType: "json",
                success: function (R) {
                    if (C) {
                        var S = R.msg;
                        if (R.valid) {
                            M.trigger("is-valid");
                            F.removeClass("invalid").addClass("valid")
                        } else {
                            M.trigger("is-invalid", R.msg);
                            F.addClass("invalid").removeClass("valid")
                        }
                    }
                },
                beforeSend: null,
                plete: function () {
                    clearTimeout(twttr.timeouts.availabilityTimeout);
                    B()
                }
            })
        }
        function J(R) {
            O = M.val();
            clearTimeout(twttr.timeouts.availabilityTimeout);
            C = O.match(Q);
            if (!C) {
                G = O;
                B();
                return
            }
            if (O == G) {
                return
            }
            L();
            twttr.timeouts.availabilityTimeout = setTimeout(P, 2000)
        }
        M.isSignupFormField({
            validateWith: function (R) {
                if (isBlank(R)) {
                    return _("Please enter a user name")
                } else {
                    P()
                }
            },
            allowInput: Q
        });
        M.keyup(function (R) {
            if (jQuery.inArray(R.keyCode, [16, 17, 18, 20, 27, 33, 34, 35, 37, 38, 39, 40, 144]) == -1) {
                if (M.val() != "") {
                    H = true
                } else {
                    M.trigger("show-info")
                }
                K();
                J()
            }
        });
        M.bind("value-changed", P);
        M.bind("custom-validate", P)
    })P
}
})

I was looking at Twitter's static scripts and noticed that all variables and functions where just 1 character long, why and how do they do this? Has it something to do with performance? If so, why don't they give all elements on their website these kind of short names, maybe 2 characters long instead of 1 to avoid any collisions.

Example:

(function (A) {
A.fn.isScreenNameField = function () {
    return this.each(function () {
        var M = A(this);
        var F = A("#signup_username_url");
        var E = A("#screen_name_info");
        var D = A("#avail_screenname_check_indicator");
        var O;
        var C;
        var I;
        var N = M.val();
        var G = N;
        var H = N != "";
        var Q = /[a-zA-Z0-9_]/;

        function K() {
            if (H) {
                F.html(M.val())
            }
        }
        function L() {
            M.trigger("show-info");
            E.hide();
            D.show()
        }
        function B() {
            E.show();
            D.hide()
        }
        function P() {
            G = O;
            jQuery.ajax({
                type: "GET",
                url: "/users/username_available",
                data: {
                    username: O
                },
                dataType: "json",
                success: function (R) {
                    if (C) {
                        var S = R.msg;
                        if (R.valid) {
                            M.trigger("is-valid");
                            F.removeClass("invalid").addClass("valid")
                        } else {
                            M.trigger("is-invalid", R.msg);
                            F.addClass("invalid").removeClass("valid")
                        }
                    }
                },
                beforeSend: null,
                plete: function () {
                    clearTimeout(twttr.timeouts.availabilityTimeout);
                    B()
                }
            })
        }
        function J(R) {
            O = M.val();
            clearTimeout(twttr.timeouts.availabilityTimeout);
            C = O.match(Q);
            if (!C) {
                G = O;
                B();
                return
            }
            if (O == G) {
                return
            }
            L();
            twttr.timeouts.availabilityTimeout = setTimeout(P, 2000)
        }
        M.isSignupFormField({
            validateWith: function (R) {
                if (isBlank(R)) {
                    return _("Please enter a user name")
                } else {
                    P()
                }
            },
            allowInput: Q
        });
        M.keyup(function (R) {
            if (jQuery.inArray(R.keyCode, [16, 17, 18, 20, 27, 33, 34, 35, 37, 38, 39, 40, 144]) == -1) {
                if (M.val() != "") {
                    H = true
                } else {
                    M.trigger("show-info")
                }
                K();
                J()
            }
        });
        M.bind("value-changed", P);
        M.bind("custom-validate", P)
    })P
}
})
Share Improve this question edited Apr 27, 2010 at 16:39 Ikke 101k23 gold badges100 silver badges120 bronze badges asked Apr 27, 2010 at 16:35 John SmithJohn Smith 633 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 19

This script has been "minified", an automated technique of replacing variables with shorter names, without changing functionality. See JSMin, for example. The goal is to reduce download times and bandwidth when sending the script to a client.

They run their scripts through something like http://developer.yahoo./yui/pressor/ in order to reduce their size, and therefore the time the need to load.

This is all done in order to decrease the websites load times.
In recent years this topic has bee something like a new field, especially due to things like the talks from Steve Stouders: http://stevesouders./

Javascript is client-side, so you have to load the script. Less text to download mean better performance, I'd think.

Many javascript projects run their code through a 'minifier' to make the code smaller. This improves the time the browser takes to download the library. Most projects also supply a non-minified version for developers to read:

Example here: http://docs.jquery./Downloading_jQuery#Current_Release

Could be many reasons as to why they do this, to name a mon one:

Decrease the filesize of the scripts as alot of people use twitter.

发布评论

评论列表(0)

  1. 暂无评论