简体   繁体   English

为什么Chrome Dev Tools检查器显示的值与代码中的值不同?

[英]Why is Chrome Dev Tools inspector showing different values than in code?

A picture is worth a thousand words. 一张图片胜过千言万语。 Maybe i'm tired I don't know, But this has me stuck for some reason and makes no sense what so ever. 也许我累了我不知道,但这让我陷入了某种原因并且没有任何意义。 Can anyone point me in the right direction? 谁能指出我正确的方向?

Chrome开发工具

IE开发工具

> Right click images and open in new tab >右键单击图像并在新选项卡中打开

Stand Alone Code 独立代码

/**
 * @file maestro.class.js
 *
 * @external $M         Main Maestro Object
 * @external $M.Core    Maestro Core Object
 */

/**
 * A modified version of class.js to cater to static inheritance and deep 
 * object cloning. Based almost completely on class.js (Javascript MVC -- 
 * Justin Meyer, Brian Moschel, Michael Mayer and others)
 * (http://javascriptmvc.com/contribute.html)
 *
 * - Some portions adapted from Prototype JavaScript framework, version 1.6.0.1 (c) 2005-2007 Sam Stephenson
 * - Some portions extracted from jQuery 1.7
 * - String utilities methods removed and added to String prototype. Documentation
 * added and making code Closure Compiler Advanced Safe by Andrew Donelson.
 *
 * Class system for javascript
 *
 * Setup method will be called prior to any init -- nice if you want to do things without needing the
 * users to call _super in the init, as well as for normalizing parameters.
 *
 * @class Class
 * @expose
 * @namespace Class
 * @memberof Core
 * Advanced Optimization Compliant
 */
(function (m$)
{
    var 
        /** 
         * I have dropped this code for production, But it's bothering as to why
         * this is happening and I would like to get it figured out.
         *
         * @property {Object} regs
         * @expose 
         */
        regs = {
            'undHash':/_|-/,
            'colons':/::/,
            'words':/([A-Z]+)([A-Z][a-z])/g,
            'lowUp':/([a-z\d])([A-Z])/g,
            'dash':/([a-z\d])([A-Z])/g,
            'replacer':/\{([^\}]+)\}/g,
            'dot':/\./
        },

        /**
         * @method getNext
         * @expose
         * @param {*} current
         * @param {string} nextPart
         * @param {boolean} add
         * @returns {*} 
         */
        getNext = function(current, nextPart, add)
        {
            return current[nextPart] || ( add && (current[nextPart] = {}) );
        },

        /**
         * Returns true if the given parameter is either a function or object
         *
         * @method isContainer
         * @expose
         * @param {*} current
         * @returns {*} 
         */
        isContainer=function (current)
        {
            var type = typeof current;
            return type && (  type == 'function' || type == 'object' );
        },

        /**
         * Gets an object from a string.
         *
         * @method getObject
         * @expose
         * @param {String} name the name of the object to look for
         * @param {Array} [roots] an array of root objects to look for the name
         * @param {Boolean} [add] true to add missing objects to
         *  the path. false to remove found properties. undefined to
         *  not modify the root object
         * @returns {String} 
         */
        getObject=function (objectName, roots, add)
        {
            //Production version:
            //var parts = objectName ? objectName.split(/\./) : [],

            var parts = objectName ? objectName.split(regs.dot) : [],
                length = parts.length,
                currents = m$.isArray(roots) ? roots : [roots || window],
                current,
                ret,
                i,
                c = 0,
                type;

            if (length == 0)
            {
                return currents[0];
            }
            while (current = currents[c++])
            {
                for (i = 0; i < length - 1 && m$.isContainer(current); i++)
                {
                    current = m$.getNext(current, parts[i], add);
                }
                if (m$.isContainer(current))
                {

                    ret = m$.getNext(current, parts[i], add);

                    if (ret !== undefined)
                    {

                        if (add === false)
                        {
                            delete current[parts[i]];
                        }
                        return ret;
                    }

                }
            }
        };

    //This is just trying to get it to save the names correctly. 
    //not really wanted. and not working anyway.
    m$.regs=regs;
    m$.getNext = getNext;
    m$.isContainer = isContainer;
    m$.getObject = getObject;   

})(M$['Core']);

And minimized with closure compiler, advanced optimizations: 使用闭包编译器进行最小化,进行高级优化:

//So why did everything get renamed? Noot only was expose used,
//but I manually accessed the objects methods and property.
(function(b){
var h={undHash:/_|-/,colons:/::/,words:/([A-Z]+)([A-Z][a-z])/g,lowUp:/([a-z\d])([A-Z])/g,dash:/([a-z\d])([A-Z])/g,replacer:/\{([^\}]+)\}/g,dot:/\./};
b.e=h;
b.a=function(a,b,f){return a[b]||f&&(a[b]={})};
b.b=function(a){return(a=typeof a)&&("function"==a||"object"==a)};
b.d=function(a,d,f){a=a?a.split(h.c):[];
var k=a.length;
d=b.isArray(d)?d:[d||window];
var c,g,e,l=0;if(0==k)return d[0];
for(;c=d[l++];){for(e=0;e<k-1&&b.b(c);e++)c=b.a(c,a[e],f);
if(b.b(c)&&(g=b.a(c,a[e],f),void 0!==g))return!1===f&&delete c[a[e]],g}}
})(M$.Core);

I really wish you'd just copy pasted the complete text somewhere (in pastebin or jsfiddle if not here) because it's much harder to answer if I keep having to click on the picture, remember what I saw, click on this window and start typing, click on the picture to remind myself, click on this window and continue typing... click on the picture to be sure etc.. 我真的希望你只是复制粘贴完整的文本(如果不在这里粘贴在pastebin或jsfiddle中),因为如果我不得不点击图片,记住我看到的内容,点击此窗口并开始输入,就更难回答,点击图片提醒自己,点击此窗口继续输入...点击图片,以确保等。

Anyway, the mysterious bit is not that f changes to J or h changes to J but that G changes to J and regs changes to J . 无论如何,神秘位不是f改变Jh改变JG改变Jregs变化J

If you look at the line below, the variables f and h are simply assigned: 如果您查看下面的行,则只需指定变量fh

a.G = f;

And from that point on f is no longer used. 从那时起f就不再使用了。 Now, considering this, I'm going to assume that at some point aG or a.regs will be assigned to Core.J . 现在,考虑到这,我会认为在某些时候aGa.regs将被分配到Core.J I'm further going to assume that the non closure compiled code is actually assigning a.regs to Core.regs but the closure compiler compiles Core.regs into Core.J and a.regs into aG . 我进一步要假定非封闭编译的代码实际上是分配a.regsCore.regs但关闭编译器编译Core.regsCore.Ja.regsaG

Search in the remainder of the code for how the values contained in a gets assigned to Core and you'll have your answer. 在其余的代码搜索包含的值是如何a被分配给Core ,你就会有你的答案。


Additional Answer: 附加答案:

If you want to force closure to export something you need to make it impossible to replace the names safely. 如果要强制关闭导出某些内容,则需要安全地替换名称。 Global variables cannot be replaced safely because they may be used by another js file included in the page. 全局变量无法安全替换,因为它们可能被页面中包含的另一个js文件使用。 As such, if you want the regs to be preserved then make it a global by removing the var . 因此,如果您希望保留regs ,则通过删除var使其成为全局。

Or, make it clear that a is global or Core or M$ is global depending on your needs. 或者,根据您的需要明确指出a是全局的还是CoreM$是全局的。

清除缓存或按ctrl + F5重新加载页面。

Okay, here is the working stand alone code for anyone else trying to make their code Closure Compiler Advanced Optimization Safe. 好的,这是其他任何试图使其代码Closure Compiler Advanced Optimization Safe的工作独立代码。 Keep in mind, Code and JSDOC/Closure Comments are required. 请记住,Code和JSDOC / Closure评论是必需的。 I used extra @expose tags but that does not hurt. 我使用了额外的@expose标签,但这并没有伤害。

Raw Source:: 原始来源::

/**
 * A modified version of class.js to cater to static inheritance and deep 
 * object cloning. Based almost completely on class.js (Javascript MVC -- 
 * Justin Meyer, Brian Moschel, Michael Mayer and others)
 * (http://javascriptmvc.com/contribute.html)
 *
 * - Some portions adapted from Prototype JavaScript framework, version 1.6.0.1 (c) 2005-2007 Sam Stephenson
 * - Some portions extracted from jQuery 1.7
 * - String utilities methods removed and added to String prototype. Documentation
 * added and making code Closure Compiler Advanced Safe by Andrew Donelson.
 *
 * Class system for javascript
 *
 * Setup method will be called prior to any init -- nice if you want to do things without needing the
 * users to call _super in the init, as well as for normalizing parameters.
 *
 * @class Class
 * @expose
 * @namespace Class
 * @memberof Core
 * Advanced Optimization Compliant
 */
(function (m$)
{
    /** 
     * Some stand regular expressions used by the internal string prototypes
     * to handle correct naming.
     *
     * @property {Object} regs
     * @expose 
     */
    m$['regs'] = {
        'undHash':/_|-/,
        'colons':/::/,
        'words':/([A-Z]+)([A-Z][a-z])/g,
        'lowUp':/([a-z\d])([A-Z])/g,
        'dash':/([a-z\d])([A-Z])/g,
        'replacer':/\{([^\}]+)\}/g,
        'dot':/\./
    },

    /**
     * @method getNext
     * @expose
     * @param {*} current
     * @param {string} nextPart
     * @param {boolean} add
     * @returns {*} 
     */
    m$['getNext'] = function(current, nextPart, add)
    {
        return current[nextPart] || ( add && (current[nextPart] = {}) );
    },

    /**
     * Returns true if the given parameter is either a function or object
     *
     * @method isContainer
     * @expose
     * @param {*} current
     * @returns {*} 
     */
    m$['isContainer'] = function (current)
    {
        var type = typeof current;
        return type && (  type == 'function' || type == 'object' );
    },

    /**
     * Gets an object from a string.
     *
     * @method getObject
     * @expose
     * @param {String} name the name of the object to look for
     * @param {Array} [roots] an array of root objects to look for the name
     * @param {Boolean} [add] true to add missing objects to
     *  the path. false to remove found properties. undefined to
     *  not modify the root object
     * @returns {String} 
     */
    m$['getObject'] = function (objectName, roots, add)
    {   
        var parts = objectName ? objectName.split(regs['dot']) : [],
            length = parts.length,
            currents = m$.isArray(roots) ? roots : [roots || window],
            current,
            ret,
            i,
            c = 0,
            type;

        if (length == 0)
        {
            return currents[0];
        }
        while (current = currents[c++])
        {
            for (i = 0; i < length - 1 && m$.isContainer(current); i++)
            {
                current = m$.getNext(current, parts[i], add);
            }
            if (m$.isContainer(current))
            {

                ret = m$.getNext(current, parts[i], add);

                if (ret !== undefined)
                {

                    if (add === false)
                    {
                        delete current[parts[i]];
                    }
                    return ret;
                }

            }
        }
    }
})(M$['Core']);

And here is the Advanced Optimization minimized code cleaned up a bit for legibility:: 这里是高级优化最小化代码清理了一点易读性::

(function(b){
b.regs={
undHash:/_|-/,
colons:/::/,
words:/([A-Z]+)([A-Z][a-z])/g,
lowUp:/([a-z\d])([A-Z])/g,
dash:/([a-z\d])([A-Z])/g,
replacer:/\{([^\}]+)\}/g,
dot:/\./
};
b.getNext=function(a,b,f){return a[b]||f&&(a[b]={})};
b.isContainer=function(a){return(a=typeof a)&&("function"==a||"object"==a)};
b.getObject=function(a,d,f){a=a?a.split(regs.dot):[];
var h=a.length;
d=b.isArray(d)?d:[d||window];
var c,g,e,k=0;if(0==h)return d[0];
for(;c=d[k++];){for(e=0;e<h-1&&b.b(c);e++)c=b.a(c,a[e],f);
if(b.b(c)&&(g=b.a(c,a[e],f),void 0!==g))return!1===f&&delete c[a[e]],g}}
})(M$.Core); // 584 Bytes

Simple Optimization minimized code cleaned up a bit for legibility:: 简单优化最小化代码清理了一点易读性::

(function(b){
b.regs={
undHash:/_|-/,
colons:/::/,
words:/([A-Z]+)([A-Z][a-z])/g,
lowUp:/([a-z\d])([A-Z])/g,
dash:/([a-z\d])([A-Z])/g,
replacer:/\{([^\}]+)\}/g,
dot:/\./
};
b.getNext=function(a,b,f){return a[b]||f&&(a[b]={})};
b.isContainer=function(a){return(a=typeof a)&&("function"==a||"object"==a)};
b.getObject=function(a,d,f){a=a?a.split(regs.dot):[];
var h=a.length;d=b.isArray(d)?d:[d||window];
var c,g,e,k=0;
if(0==h)return d[0];
for(;c=d[k++];){for(e=0;e<h-1&&b.isContainer(c);e++)c=b.getNext(c,a[e],f);
if(b.isContainer(c)&&(g=b.getNext(c,a[e],f),void 0!==g))return!1===f&&delete c[a[e]],g}}
})(M$.Core); //616 bytes

So, is it worth the headache and extra hassle to make your code compile in Advanced Optimizations? 那么,在高级优化中编译代码是否值得头痛和麻烦?

I say Yes and No and I'll explain... 我说是和否,我会解释......

  • Yes if: You plan your structure ahead of time and use proper Closures with all of your internal helper or utility methods added to another closure inside of your main closure. 是的,如果:您提前计划结构并使用正确的Closures,并将所有内部帮助器或实用程序方法添加到主闭包内的另一个闭包中。 This way you don't clutter up your main object. 这样你就不会弄乱你的主要对象。

  • No if: Standard unorganized code. 否:如果:标准无组织代码。 Nothing wrong with this at all, but it is not worth wasting your time to do advanced optimizations. 这根本没有错,但是不值得浪费你的时间来进行高级优化。

Well, let me know what you think. 好吧,让我知道你的想法。 and thanks for the input and help. 并感谢您的投入和帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM