dcsimg
Results 1 to 13 of 13

Thread: [RESOLVED] A strange JavaScript code

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Resolved [RESOLVED] A strange JavaScript code

    I saw a piece of JavaScript code and I thought it was a bit weird.

    Code:
    var myBox = new box();
    function box() {
        this.dx = 0;
        this.dy = 0;
        this.test = function(a, b, c){
            var div = box.test2(a, b, c);
            if (div) {
                myBox.dx = 400;
                myBox.dy = 600;
            }	
        }
    }
    I understand this code this way: box is a function object(class), and variable myBox is an instance of the box object(class). But the myBox variable appears inside the box, which makes me feel a bit strange.

    Is this usage of myBox appropriate? Will skilled or sophisticated JavaScript developers write code in this way? Is there a better way to achieve similar functionality? Thanks.

    Note:
    Although this question has nothing to do with VB6, I'm learning and using JS with the thinking of an old VB6 user, so I still want to put it in the VB6 sub-forum.
    Last edited by dreammanor; Oct 22nd, 2019 at 07:07 PM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,859

    Re: A strange JavaScript code

    That function makes little sense to me. myBox is an unknown entity inside the BOX object.

    And .test2 does not exist anywhere.

    Here is a little class I've got that serializes values

    Code:
            var serial_maker = function() {
                var prefix = '';
                var seq = 0;
                return {
                    set_prefix: function(p) {
                        prefix = String(p);
                    },
                    set_seq: function(s) {
                        seq = s;
                    },
                    gensym: function() {
                        var result = prefix + seq;
                        seq += 1;
                        return result;
                    }
                };
            };
    and three instances of that class/object

    Code:
            var seqEditPanel = serial_maker();
            seqEditPanel.set_prefix('acs-seqEditPanel-');
    
            var seqDropDown = serial_maker();
            seqDropDown.set_prefix('acs-seqDropDown-');
    
            var tabOrder = serial_maker();
            tabOrder.set_prefix('');
    And then using it later

    Code:
    var strSeq = seqDropDown.gensym();
    Of course there is CLOSURE which allows objects to retain "values" and keep them from being discarded - but that's not what I see in the example you posted.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,232

    Re: A strange JavaScript code

    Very strange, I thought I posted a reply. Here's what I was going to post:

    Now it is in both forums, with a link between them. Asking VB6 folks about an explicitly JS question is a bit of a stretch.

    Is that code working? JS allows you to do all kinds of weird things without complaining too much. Doesn't mean it's right to do, though. I was going to point out how strange that was, but since the post didn't, szlamany beat me to it, and did so more thoroughly.
    My usual boring signature: Nothing

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,859

    Re: A strange JavaScript code

    Code:
    var myBox = new box();
    function box() {
        this.dx = 0;
        this.dy = 0;
        this.test = function(a, b, c){
            var div = box.test2(a, b, c);
            if (div) {
                myBox.dx = 400;
                myBox.dy = 600;
            }	
        }
    }
    I just looked at this again, and now realize it's just really sloppy code.

    The variable myBox is actually on the "window" object - it's window.myBox in reality. VB programmers would think of this as a global variable. In a way it is, since "window" is an object that everything can see and it's also the "hidden default" object that myBox goes onto.

    With that said, sure, you can create a function called box. And you can assign that to a new object called myBox.

    But if you refer to "myBox" from within "box" you are talking to window.myBox. This works as long as you only have ONE myBox.

    When you create var myBox2 = new box(); and use it - even though it's called myBox2 - when you SET DX and DY you are setting them on the myBox instance!

    Serious bug!!

    The important thing about what you see in my example is that you can have PRIVATE variables and PUBLIC methods - those things I am returning are "public" methods. There can also be constructor code - I just happen to have none in this simple function. It would simply go before the RETURN statement.

    You can almost see how the code in the OP was created as a simple test code and then dropped into a function of some type and then into this class like object. Attention needed to be paid at each step of this growth!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: A strange JavaScript code

    Quote Originally Posted by szlamany View Post
    That function makes little sense to me. myBox is an unknown entity inside the BOX object.

    And .test2 does not exist anywhere.

    Here is a little class I've got that serializes values

    Code:
            var serial_maker = function() {
                var prefix = '';
                var seq = 0;
                return {
                    set_prefix: function(p) {
                        prefix = String(p);
                    },
                    set_seq: function(s) {
                        seq = s;
                    },
                    gensym: function() {
                        var result = prefix + seq;
                        seq += 1;
                        return result;
                    }
                };
            };
    and three instances of that class/object

    Code:
            var seqEditPanel = serial_maker();
            seqEditPanel.set_prefix('acs-seqEditPanel-');
    
            var seqDropDown = serial_maker();
            seqDropDown.set_prefix('acs-seqDropDown-');
    
            var tabOrder = serial_maker();
            tabOrder.set_prefix('');
    And then using it later

    Code:
    var strSeq = seqDropDown.gensym();
    Of course there is CLOSURE which allows objects to retain "values" and keep them from being discarded - but that's not what I see in the example you posted.
    Hi szlamany, I really like your style of writing JS code. Thank you very much.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: A strange JavaScript code

    Quote Originally Posted by Shaggy Hiker View Post
    Very strange, I thought I posted a reply. Here's what I was going to post:

    Now it is in both forums, with a link between them. Asking VB6 folks about an explicitly JS question is a bit of a stretch.

    Is that code working? JS allows you to do all kinds of weird things without complaining too much. Doesn't mean it's right to do, though. I was going to point out how strange that was, but since the post didn't, szlamany beat me to it, and did so more thoroughly.
    Hi Shaggy Hiker, what you said is very reasonable, thank you.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: A strange JavaScript code

    Quote Originally Posted by szlamany View Post
    I just looked at this again, and now realize it's just really sloppy code.
    ...
    ...
    Attached is the JS Code I'm learning, which uses a lot of the writing methods mentioned in my first post, for example:

    Code:
    var cmn = new cmnc();
    function cmnc() {
        this.ComponentName = 'APNSoft WebControls JS source file.';
        this.Version = 'Version 4.1 (34)';
        this.Copyright = 'Copyright (C) APNSoft. All rights reserved.';
        this.uid = null;
        ...
        ...
        this.BdX = 0;
        this.BdY = 0;
        this.ErrHr = function(ex, args) {
            try {
                var wen = cmn.Gisv('wen');
                if (wen != 'true') return;
                var msg = 'Error! APNSoft Control was unable to perform an operation.';
                if (ex.lineNumber) {
                    msg += '\r\nLine Number: ' + ex.lineNumber;
                }
                ...
                ...
            }
            ...
            ...
    }
    Code:
    var dm = new dmc();
    function dmc() {
        ...
        ...
        this.DsplX = 0;
        this.DsplY = 0;
        ...
        ...
        this.i = function(mid, t, wbfnt) {
            try {
                if (wbfnt == null || wbfnt == undefined) wbfnt = '';
                var div = cmn.Mko(mid + '_-0p');
                var uid_l = cmn.Gisv('uid', mid);
                if ((uid_l == '' || div == null) && t < 50) {
                    t++;
                    setTimeout('dm.i(\'' + mid + '\',' + t + ',\'' + wbfnt + '\');', 50);
                    return;
                }
                if (div) {
                    dm.IsMob = cmn.IsMobChk();
                    if (dm.IsMob == true) {
                        cmn.Sisv('smc', '2', mid);
                    }
                    dm.uid = mid;
                    dm.dInO(mid);
                    dm.HSPth(mid);
                    dm.mO = cmn.Gisv('o', mid);
                    ...
                    ...
                }
                ...
                ...
            }
            ...
            ...
    }
    Could you comment on the code style (or even the code structure) in the attachment is standard and reasonable? Thanks.
    Last edited by dreammanor; Oct 24th, 2019 at 06:46 PM.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,859

    Re: A strange JavaScript code

    I can look further tomorrow...

    Any place you see an "internal" function referring to the calling member (wow - omg!) that should always be replaced with "this" (nearly always - that's part of the understanding).

    JS requires a bit of assembler/machine code experience to understand referencing...

    Seems from the code you posted that DEBUG logic was maybe inferred to be good coding and pushed up the stack to real code?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: A strange JavaScript code

    Quote Originally Posted by szlamany View Post
    I can look further tomorrow...
    OK,thank you, szlamany.

    Quote Originally Posted by szlamany View Post
    Any place you see an "internal" function referring to the calling member (wow - omg!) that should always be replaced with "this" (nearly always - that's part of the understanding).
    I also considered replacing the class instance variable "dm" or "cmn" with "this". But I found a phenomenon, that is, the class instance variable "dm" or "cmn" seems to be more readable than "this", especially in the messy JS code. Because I can think of "dm" or "cmn" as a global variable, just like the global variables of VB6.

    Quote Originally Posted by szlamany View Post
    JS requires a bit of assembler/machine code experience to understand referencing...
    I know a little bit of assembly knowledge. But I wrote too much VB6 code, so that the programming thinking of VB6 is deeply imprinted in my brain. JS has too many weird and imperfect in the early days, which makes me always despise it and has been reluctant to learn it. But now, if a developer doesn't learn JS, he is destined to be abandoned by the Internet.

    Quote Originally Posted by szlamany View Post
    Seems from the code you posted that DEBUG logic was maybe inferred to be good coding and pushed up the stack to real code?
    My Web-Builder needs a MenuBuilder. Initially I was planning to use the pure CSS Menu Builder solution. But pure JS MenuBuilder seems to have more flexibility. So I found CommonMenu.JS on the Internet, I want to understand its principles and logic, and then write a pure JS MenuBuilder of my own. My JS skills are very poor. The biggest obstacle I faced was the inability to debug JS efficiently. I only know that debugging JS in Chrome, which is extremely inconvenient. So I have been trying to write a JS debugging tool like the VB6 IDE (there is no progress yet).
    Last edited by dreammanor; Oct 22nd, 2019 at 11:52 PM.

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,859

    Re: A strange JavaScript code

    Get this book: JavaScript: The Good Parts by Douglas Crockford.

    Back to the code sample you posted.

    I see a heavy use of "this" for declaring all those variables up top, instead of using, for example, var ComponentName = 'APNSoft ...'. I would imagine that was to insure that each variable declared is unique - not trampling on an existing global variable. Even that function - ErrHr - is defined on "this". Since this library is expected to be included in a larger pile of JS, this does make sense.

    Code:
    function cmnc() {
        this.ComponentName = 'APNSoft WebControls JS source file.';
        this.Version = 'Version 4.1 (34)';
        this.Copyright = 'Copyright (C) APNSoft. All rights reserved.';
        this.uid = null;
        this.mX = 0;
        this.mY = 0;
    .
    .
    .
        this.ErrHr = function(ex, args) {
            try {
    Now - let's look at the use of the "instantiated" object inside the cmnc() class.

    The code sample they give you has the class coming to life in the variable cmn. There should be a comment in the code stating that the name of this variable is important - since we have references in the class code to the cmn variable. And the fact that you CANNOT have two instances of this class. And even more important, the new keyword is REQUIRED when making this instance, as without it this would be referring to the parent. In other words this === window if new is left off.

    I guess the programmers were of the opinion that the "functions" and "variables" declared on the this object inside the class cannot be easily referenced by this inside the functions themselves, since this gets re-assigned (?), I would have fought harder to craft a better solution. So, someone came up with referencing cmn inside all those internal functions. Interesting way to accomplish this.

    Code:
    var cmn = new cmnc();
    function cmnc() {
        this.ComponentName = 'APNSoft WebControls JS source file.';
        this.Version = 'Version 4.1 (34)';
        this.Copyright = 'Copyright (C) APNSoft. All rights reserved.';
    .
    .
    .
        this.ErrHr = function(ex, args) {
            try {
                var wen = cmn.Gisv('wen');
                if (wen != 'true') return;
                var msg = 'Error! APNSoft Control was unable to perform an operation.';
                if (ex.lineNumber) {
                    msg += '\r\nLine Number: ' + ex.lineNumber;
                }
    .
    .
    .
        this.Gisv = function(key, uid) {
            var UID = cmn.uid;
            if (uid != null && uid != undefined) {
                UID = uid;
            }
            var val = '';
    Since I write classes in JS for my own consumption, I am careful about variable naming conventions - look at this code - a class for managing a grid of data. Lots of internal functions (all with i_xxxx names) and then I return a bunch of public methods and what not that call those internal functions. No need to worry about this references, since I am consuming this class in my own code. I make dozens of instances of this class based on how many TABS my users open in my web app. I always name global variables as g_xxxxx - you will see some references to those.

    I also put all VAR statements at the top. This is not required - but it's proper coding practice.

    Code:
            var grid_maker = function () {
                var gmOffset = -1;
                var gmNameSeq = 0;
                var windowOn = false;
                var parentInfo = {};
    
                var i_set_current = function (i) {
                    var intGO = g_objGMaker[gmOffset].parentInfo.baseGO;
                    g_objGrid[intGO].reader.gminfo.gmcurrent = i;
                    i_show_names(i);
                };
                var i_clear_popup = function (i) {
                    for (var j = 0; j < g_objGMaker.length; j++) {
                        if (g_objGMaker[j].parentInfo.baseGO == i) {
                            g_objGMaker[j].status = "deleted";
                            g_objGMaker[j].gm = -1;
                            g_objGMaker[j].parentInfo.baseGO = -1;
                            g_objGMaker[j].parentInfo.GO = -1;
                            g_objGMaker[j].gmSource.sourceOrig = [];
                            g_objGMaker[j].gmSource.sourceOutput = [];
                            g_objGMaker[j].gmSource.sourceSummary = [];
                            g_objGMaker[j].gmProperties.settingsObj = {GO: -1};
                        }
                    }
                };
                var i_set_filtermenu = function (i, blnPopup) {
                    var ggmOff = gmOffset;
                    var gotSetup = false;
                    var baseGO = 0;
                    var gotNew = false;
                    var gotReport = false;
                    var gotInfo = false;
                    if (i != null) {
                        ggmOff = i;
                    }
                    baseGO = g_objGMaker[ggmOff].parentInfo.baseGO;
                    for (var j = 0; j < g_objGMaker.length; j++) {
                        gotSetup = false;
                        if ((g_objGMaker[j].parentInfo.baseGO == baseGO)) {
                            gotNew = (g_objGMaker[j].status == "new");
                            gotReport = (g_objGMaker[j].status == "load") && g_objGMaker[j].gmProperties.rptSetup;
                            gotInfo = gotReport && g_objGMaker[j].gmProperties.rptInfo;
                            if (!gotNew && !gotReport) {
                                gotSetup = true;
                                if (blnPopup) {
                                    break;
                                }
                            } else if (gotInfo) {
                                gotSetup = true;
                            }
                        }
                    }
                    if (gotSetup) {
                        $(g_objGMaker[ggmOff].parentInfo.parent + '-headingfilter').addClass('acs-filter-menu-trigger').data("ggmOff", ggmOff);
                    }
                };
                var i_set_rptmenu = function (i) {
                    var ggmOff = gmOffset;
                    var gotSetup = true;
                    var wesBtn = [];
                    if (i != null) {
                        ggmOff = i;
                    }
                    if (g_objGMaker[ggmOff].gmProperties.rptSproc.length == 0) {
                        gotSetup = false;
                    }
                    if (g_objGMaker[ggmOff].gmProperties.rptBind.length == 0) {
                        gotSetup = false;
                    }
                    if (gotSetup) {
                        g_objGMaker[ggmOff].gmProperties.rptSetup = true;
                        if (!g_objGMaker[ggmOff].gmProperties.rptInfo) {
                            wesBtn = $(g_objGMaker[ggmOff].parentInfo.parent + '-headingprint');
                            if (!wesBtn.hasClass('acs-print-menu-trigger')) {
                                wesBtn.addClass('acs-print-menu-trigger').data("ggmOff", ggmOff).data("rptselect", String(ggmOff)); //"-1");
                            }
                        }
                    } else {
                        g_objGMaker[ggmOff].gmProperties.rptSetup = false;
                    }
                };
    .
    .
    .
    Code:
                return {
                    get_fid: function () {
                        return g_objGMaker[gmOffset].fid;
                    },
                    create: function (s) {
                        var sName = "";
                        if (s == null) {
                            gmNameSeq += 1;
                            sName = "Filter Setup #" + String(gmNameSeq); //gmOffset + 1);
                        } else {
                            sName = s;
                        }
                        gmOffset = g_objGMaker.length;
                        g_objGMaker.push({ status: "init", gm: gmOffset, gname: sName, filterTab: -1, gmProperties: {}, parentInfo: {}, gmSource: {} });
                        g_objGMaker[gmOffset].gmProperties.summaryOn = false;
                        g_objGMaker[gmOffset].gmProperties.hideCount = false;
                        g_objGMaker[gmOffset].gmProperties.filterOn = false;
                        g_objGMaker[gmOffset].gmProperties.filterApply = false;
                        g_objGMaker[gmOffset].gmProperties.arrFilters = [];
                        g_objGMaker[gmOffset].gmProperties.curFilter = {};
                        g_objGMaker[gmOffset].gmProperties.curFilterIndex = -1;
                        g_objGMaker[gmOffset].gmProperties.curFilterDelete = false;
                        g_objGMaker[gmOffset].gmProperties.hideCols = false;
                        g_objGMaker[gmOffset].gmProperties.rptSetup = false;
                        g_objGMaker[gmOffset].gmProperties.rptSproc = "";
                        g_objGMaker[gmOffset].gmProperties.rptSubmit = "";
                        g_objGMaker[gmOffset].gmProperties.rptBind = "";
                        g_objGMaker[gmOffset].gmProperties.rptInfo = false;
                        g_objGMaker[gmOffset].gmProperties.defaultSetup = false;
                        g_objGMaker[gmOffset].gmProperties.settingsObj = {};
                        g_objGMaker[gmOffset].gmProperties.strSortPanel = "";
                        g_objGMaker[gmOffset].gmProperties.strFilterPanel = "";
                        g_objGMaker[gmOffset].gmProperties.strReportPanel = "";
                        g_objGMaker[gmOffset].gmProperties.strWindow = "";
                        g_objGMaker[gmOffset].gmProperties.strSelectSort = "";
                        g_objGMaker[gmOffset].gmProperties.strSelectTotal = "";
                        g_objGMaker[gmOffset].gmProperties.strTotalPanel = "";
                        g_objGMaker[gmOffset].gmProperties.strSelectFilter = "";
                        g_objGMaker[gmOffset].gmProperties.strSelectGraph = "";
                        g_objGMaker[gmOffset].gmProperties.strGraphPanel = "";
                        g_objGMaker[gmOffset].gmProperties.strList = "";
                        g_objGMaker[gmOffset].gmProperties.strMessage = "";
                        g_objGMaker[gmOffset].gmProperties.messagepump = [];
                        g_objGMaker[gmOffset].gmProperties.lastfilter = "";
                        g_objGMaker[gmOffset].gmProperties.showlist = false;
                        g_objGMaker[gmOffset].gmProperties.curGraph = { graphColumn: "", nameColumns: "", title: "", key: "", includeHeadings: false, acsGraph: {} };
                        return gmOffset;
                    },
                    clone: function (i, s) {
                        var sName = "";
                        if (s == null) {
                            gmNameSeq += 1;
                            sName = "Filter Setup #" + String(gmNameSeq); //gmOffset + 1);
                        } else {
                            sName = s;
                        }
                        gmOffset = g_objGMaker.length;
                        g_objGMaker.push({ status: "clone", clone: i, gm: gmOffset, gname: sName, filterTab: -1, gmProperties: {}, parentInfo: {}, gmSource: {} });
                        g_objGMaker[gmOffset].gmSource.sourceOrig = g_objGMaker[i].gmSource.sourceOrig;
                        g_objGMaker[gmOffset].gmSource.sourceOutput = g_objGMaker[i].gmSource.sourceOutput.slice(0);
                        g_objGMaker[gmOffset].gmSource.sourceOutput.gm = gmOffset;
                        mergeObjectsDeep(g_objGMaker[gmOffset].parentInfo, g_objGMaker[i].parentInfo);
                        g_objGMaker[gmOffset].parentInfo.original = false;
                        mergeObjectsShallow(g_objGMaker[gmOffset].gmProperties, g_objGMaker[i].gmProperties);
                        mergeObjectsShallow(g_objGMaker[gmOffset].gmProperties.settingsObj, g_objGMaker[i].gmProperties.settingsObj);
                        mergeObjectsShallow(g_objGMaker[gmOffset].gmProperties.curGraph, g_objGMaker[i].gmProperties.curGraph);
                        for (var j = 0; j < g_objGMaker[i].gmProperties.arrFilters.length; j++) {
                            g_objGMaker[gmOffset].gmProperties.arrFilters.push({});
                            mergeObjectsShallow(g_objGMaker[gmOffset].gmProperties.arrFilters[g_objGMaker[gmOffset].gmProperties.arrFilters.length - 1], g_objGMaker[i].gmProperties.arrFilters[j]);
                            g_objGMaker[gmOffset].gmProperties.arrFilters[g_objGMaker[gmOffset].gmProperties.arrFilters.length - 1].alphaCountList = g_objGMaker[i].gmProperties.arrFilters[j].alphaCountList.slice(0);
                            mergeObjects(g_objGMaker[gmOffset].gmProperties.arrFilters[g_objGMaker[gmOffset].gmProperties.arrFilters.length - 1].alphaCountUsage, g_objGMaker[i].gmProperties.arrFilters[j].alphaCountUsage);
                            g_objGMaker[gmOffset].gmProperties.arrFilters[g_objGMaker[gmOffset].gmProperties.arrFilters.length - 1].dataList = g_objGMaker[i].gmProperties.arrFilters[j].dataList.slice(0);
                            mergeObjects(g_objGMaker[gmOffset].gmProperties.arrFilters[g_objGMaker[gmOffset].gmProperties.arrFilters.length - 1].dataUsage, g_objGMaker[i].gmProperties.arrFilters[j].dataUsage);
                            mergeObjectsArray(g_objGMaker[gmOffset].gmProperties.arrFilters[g_objGMaker[gmOffset].gmProperties.arrFilters.length - 1].filter, g_objGMaker[i].gmProperties.arrFilters[j].filter);
    
                        }
                        if (g_objGMaker[gmOffset].gmProperties.settingsObj.colsum) {
                            mergeObjectsArray(g_objGMaker[gmOffset].gmProperties.settingsObj.colsum, g_objGMaker[i].gmProperties.settingsObj.colsum);
                        }
                        if (g_objGMaker[gmOffset].gmProperties.settingsObj.selectedcols) {
                            mergeObjectsArray(g_objGMaker[gmOffset].gmProperties.settingsObj.selectedcols, g_objGMaker[i].gmProperties.settingsObj.selectedcols);
                        }
                        if (g_objGMaker[gmOffset].gmProperties.settingsObj.arrcontrol) {
                            g_objGMaker[gmOffset].gmProperties.settingsObj.arrcontrol = g_objGMaker[i].gmProperties.settingsObj.arrcontrol.slice(0);
                        }
                        if (g_objGMaker[gmOffset].gmProperties.settingsObj.columns) {
                            g_objGMaker[gmOffset].gmProperties.settingsObj.columns = g_objGMaker[i].gmProperties.settingsObj.columns.slice(0);
                        }
                        if (g_objGMaker[gmOffset].gmProperties.messagepump) {
                            g_objGMaker[gmOffset].gmProperties.messagepump = g_objGMaker[i].gmProperties.messagepump.slice(0);
                        }
                        i_set_filtermenu(gmOffset, false);
                        return gmOffset;
                    },
                    copy: function (i1, i2) {
                        g_objGMaker[i1].gmProperties.strSelectSort = g_objGMaker[i2].gmProperties.strSelectSort;
                        g_objGMaker[i1].gmProperties.strSelectTotal = g_objGMaker[i2].gmProperties.strSelectTotal;
                        g_objGMaker[i1].gmProperties.strTotalPanel = g_objGMaker[i2].gmProperties.strTotalPanel;
                        g_objGMaker[i1].gmProperties.strSelectFilter = g_objGMaker[i2].gmProperties.strSelectFilter;
                        g_objGMaker[i1].gmProperties.strSelectGraph = g_objGMaker[i2].gmProperties.strSelectGraph;
                        g_objGMaker[i1].gmProperties.strList = g_objGMaker[i2].gmProperties.strList;
                        g_objGMaker[i1].gmProperties.strSortPanel = g_objGMaker[i2].gmProperties.strSortPanel;
                        g_objGMaker[i1].gmProperties.strFilterPanel = g_objGMaker[i2].gmProperties.strFilterPanel;
                        g_objGMaker[i1].gmProperties.strGraphPanel = g_objGMaker[i2].gmProperties.strGraphPanel;
                        g_objGMaker[i1].gmProperties.strWindow = g_objGMaker[i2].gmProperties.strWindow;
                        g_objGMaker[i1].gmProperties.strReportPanel = g_objGMaker[i2].gmProperties.strReportPanel;
                        mergeObjectsShallow(g_objGMaker[i1].gmProperties.settingsObj, g_objGMaker[i2].gmProperties.settingsObj);
                    },
                    clear_popup: function (i) {
                        i_clear_popup(i);
                    },
                    save_properties: function () {
                        var rtnProps = {};
                        var mpItems = 0;
                        rtnProps.arrFilters = g_objGMaker[gmOffset].gmProperties.arrFilters.slice(0);
                        rtnProps.filterApply = g_objGMaker[gmOffset].gmProperties.filterApply;
                        rtnProps.filterOn = g_objGMaker[gmOffset].gmProperties.filterOn;
                        rtnProps.lastFilter = g_objGMaker[gmOffset].gmProperties.lastFilter;
                        rtnProps.hideCols = g_objGMaker[gmOffset].gmProperties.hideCols;
                        rtnProps.summaryOn = g_objGMaker[gmOffset].gmProperties.summaryOn;
                        rtnProps.hideCount = g_objGMaker[gmOffset].gmProperties.hideCount;
                        rtnProps.rptSetup = g_objGMaker[gmOffset].gmProperties.rptSetup;
                        rtnProps.rptSproc = g_objGMaker[gmOffset].gmProperties.rptSproc;
                        rtnProps.rptSubmit = g_objGMaker[gmOffset].gmProperties.rptSubmit;
                        rtnProps.rptBind = g_objGMaker[gmOffset].gmProperties.rptBind;
                        rtnProps.rptInfo = g_objGMaker[gmOffset].gmProperties.rptInfo;
                        rtnProps.defaultSetup = g_objGMaker[gmOffset].gmProperties.defaultSetup;
                        rtnProps.curGraph = g_objGMaker[gmOffset].gmProperties.curGraph;
                        rtnProps.graphOn = g_objGMaker[gmOffset].gmProperties.graphOn;
                        for (var i = 0; i < rtnProps.arrFilters.length; i++) {
                            rtnProps.arrFilters[i].alphaCountList = [];
                            rtnProps.arrFilters[i].alphaCountUsage = {};
                            rtnProps.arrFilters[i].alphaCountUse = {};
                            rtnProps.arrFilters[i].dataList = [];
                            rtnProps.arrFilters[i].dataUsage = {};
                            rtnProps.arrFilters[i].dataUsage = {};
                        }
                        if (g_objGMaker[gmOffset].gmProperties.messagepump.length == 0) {
                            rtnProps.messagepump = [];
                        } else {
                            mpItems = g_objGMaker[gmOffset].gmProperties.messagepump.length - 2;
                            if (mpItems < 0) {
                                mpItems = 0;
                            }
                            rtnProps.messagepump = g_objGMaker[gmOffset].gmProperties.messagepump.slice(mpItems);
                        }
                        rtnProps.settingsObj = {};
                        rtnProps.settingsObj.colsum = g_objGMaker[gmOffset].gmProperties.settingsObj.colsum.slice(0)
                        rtnProps.settingsObj.colsel = g_objGMaker[gmOffset].gmProperties.settingsObj.colsel.slice(0)
                        rtnProps.settingsObj.selectedcols = g_objGMaker[gmOffset].gmProperties.settingsObj.selectedcols.slice(0)
    
                        return $.toJSON(rtnProps);
                    },
                    load_fill: function (i1) {
                        i_load_fill(i1);
                    },
                    set_filter_tab: function (i) {
                        g_objGMaker[gmOffset].filterTab = i;
                    },
                    get_filter_tab: function () {
                        return g_objGMaker[gmOffset].filterTab;
                    },
                    grid_work: function (ggmOff, intGO, gwCmd) {
                        return i_grid_work(ggmOff, intGO, gwCmd);
                    },
                    push_source: function (o) {
                        g_objGMaker[gmOffset].gmSource.sourceOrig.push(o);
                        g_objGMaker[gmOffset].gmSource.sourceOutput.push(o);
                        for (var i = 0; i < g_objGMaker.length; i++) {
                            if ((i != gmOffset) && (g_objGMaker[i].gmGotPull || false) && (g_objGMaker[i].gmPull == gmOffset)) {
                                //g_objGMaker[i].gmSource.sourceOrig.push(o);
                                g_objGMaker[i].gmSource.sourceOutput.push(o);
                            }
                        }
                    },
                    get_source: function () {
                        return g_objGMaker[gmOffset].gmSource.sourceOutput;
                    },

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: A strange JavaScript code

    Very valuable comments. Much appreciated.

    When using VB6, I'm very concerned about the naming convention of variables and the style of writing code. But I have never known how to build a set of JS coding specifications and coding styles that are suitable for me. Now your experience and suggestions have helped me a lot.

    Also, from your comments, can I think that it makes sense to use its instance variable "cmn" inside a class, should I learn this programming style? Thanks.


    Edit:
    I'm wondering if it will be better to change "cmn" to "g_cmn" or "g_objCmn"?
    Last edited by dreammanor; Oct 23rd, 2019 at 07:22 PM.

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,859

    Re: A strange JavaScript code

    Quote Originally Posted by dreammanor View Post
    Also, from your comments, can I think that it makes sense to use its instance variable "cmn" inside a class, should I learn this programming style? Thanks.
    I have never needed to do something like this. As I said, I can name my variables and functions inside the class as something I know to be unique in the whole of the JavaScript code that is loaded into the browser. As long as they are unique, I know that I can reference them safely. If you look at the code sample I gave you, all those internal functions are named as i_xxxxxxx and I return methods to the outside that in turn call those internal functions. Most of the variables being manipulated in that code sample are g_xxxxx globals to my app - although there are a handful of var definitions up top.

    Code:
            var grid_maker = function () {
                var gmOffset = -1;
                var gmNameSeq = 0;
                var windowOn = false;
                var parentInfo = {};
    
                var i_set_current = function (i) {
                    var intGO = g_objGMaker[gmOffset].parentInfo.baseGO;
                    g_objGrid[intGO].reader.gminfo.gmcurrent = i;
                    i_show_names(i);
                };
                var i_clear_popup = function (i) {
                    for (var j = 0; j < g_objGMaker.length; j++) {
    Quote Originally Posted by dreammanor View Post
    I'm wondering if it will be better to change "cmn" to "g_cmn" or "g_objCmn"?
    Yes, I would rename those very important variables to something along the lines of g_. I like the idea of g_objCmn - having the "datatype" in the variable name is helpful in JS.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: A strange JavaScript code

    Ok, I got it. Thank you very much, szlamany.
    Last edited by dreammanor; Oct 24th, 2019 at 06:35 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width