Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts

    Need explanation on possible scope issue

    I have devised the following constructor based loosely on the observer pattern
    Code:
    Observer = function (ConditionIsTrue , codeToExecute){
                
                var
                    observer = this ,
                    ConditionWasMet = false ,
                    
                    CheckIfReady = function () {
                    
                        if (ConditionIsTrue()) {
                            
                            if(!ConditionWasMet)
                                codeToExecute();
                            
                            ConditionWasMet = true;
                        
                        }
                        else {
                            if (ConditionWasMet) ConditionWasMet = false;
                        }
                    };
                
                Loop.call(observer , CheckIfReady)
                observer.speed(1)
            
            };
    It works fine, no problems that I know of...

    But the most interesting thing happened when I attached it to my lib and ran it through the http://closure-compiler.appspot.com/home compiler
    Code:
    $['Observer'] = function (//...etc...
    For the first time the compiler has added something to my global scope
    Code:
    var i=!1;
    window.$=function(){//...etc...
    ...
    Observer:function(j,k){var h=i;Loop.call(this,function(){j()?(h||k(),h=!0):h&&(h=i)});this.speed(1)}
    }
    })();
    Can someone explain to me why the var ConditionWasMet had to be exported to the global scope?
    Was it already in the global scope? That would confuse me considering the observer can be called from multiple instances without conflict...
    But I don't want any surprise conflicts jumping out at me later, I appreciate the consideration.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by blaze4218 View Post

    the most interesting thing happened when I attached it to my lib and ran it through the http://closure-compiler.appspot.com/home compiler

    Can someone explain to me why the var ConditionWasMet had to be exported to the global scope?
    Why not show the code that
    you "ran it through the http://closure-compiler.appspot.com/home compiler"

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    well...
    the whole code is considerably larger in size,
    the relevant code is here (I tried this and other variations which all met the same fate),
    the actual output of the compiler was in my post, save 1 renamed variable for brevity,
    I have successfully narrowed the generation of the exported global variable to ConditionWasMet via trial and error (removing the lines ConditionWasMet = true; and if (ConditionWasMet) ConditionWasMet = false; removed the global var),
    many contributors here shun posting irrelevant code...

    But if you think it will help
    Code:
    window["BW"] = function() {
        "use strict";
    
        var Loop = function(myFunct) {
            "Private"; var
                loop = this ,
                rate = 1E3 ,
                code = myFunct ,
                paused = false ,
                run = function() {
                    clearTimeout(loop.Cycle);
                    if(code) {
                        if(!paused) {
                            code()
                        }
                        loop.Cycle = setTimeout(run , rate)
                    }
                };
            "Public";
                loop["resume"] = function() {
                    paused = false;
                } ,
                loop["pause"] = function() {
                    paused = true;
                } ,
                loop["die"] = function() {
                    code = false;
                } ,
                loop["speed"] = function(r) {
                    r && typeof r == "number" && r > 0 && (rate = r);
                    return rate;
                } ,
                loop["init"] = function() {
                    if(!loop.Cycle && code) {
                        run();
                    }
                }
        } ,
        ID = function(e) {
            return"object" === typeof e ? e : document.getElementById(e);
        } ,
        CSS = function(e) {
            if(!ID(e)) {
                return false;
            }
            return ID(e).style;
        } ,
        HTML = function(e , s , q) {
            var tmp;
            q ? (tmp = document.createElement("div") , tmp.innerHTML = s , ID(e).appendChild(tmp)) : ID(e).innerHTML = s;
        } ,
        AddSpace = function(b) {
            var s = "";
            for(var i = 0;i < b;++i) {
                s += "&nbsp;"
            }
            return s;
        } ,
        Strip = function(e) {
            ID(e).innerHTML = ID(e).innerHTML.replace(/\s*</gi , "<");
        } ,
        Clean = function() {
            return{
                "Alpha":function(v) {
                    return v.replace(/[^a-zA-Z]/g , "");
                } ,
                "Numeric":function(v) {
                    return v.replace(/[^0-9]/g , "");
                } ,
                "AlphaNumeric":function(v) {
                    return v.replace(/[^0-9a-zA-Z]/g , "");
                } ,
                "Email":function(v) {
                }
            }
        }() ,
        Time = function() {
            return new Date;
        } ,
        AssertArr = function(a) {
            return isArray(a) ? a : [a];
        } ,
        GetClass = function(c) {
            var a = 
                d = 
                b = [];
            a = document.getElementsByTagName("*");
            for(var i = 0;i < a.length;++i) {
                b = a[i].className.split(" ");
                for(var j = 0;j < b.length;++j) {
                    b[j] == c && d.push(a[i]);
                }
            }
            return d;
        } ,
        AddClass = function(id , newClass) {
            var existingClasses = [] ,
                AllId = AssertArr(id);
    
            for(var i = 0;i < AllId.length;++i) {
                var id = ID(AllId[i]);
                existingClasses = id.className.split(" ");
                for(var j = 0;j < existingClasses.length;++j) {
                    if(existingClasses[j] == newClass) {
                        return;
                    }
                }
                existingClasses.push(newClass);
                id.className = existingClasses.join(" ");
            }
        } ,
        RemoveClass = function(id , oldClass) {
            var existingClasses = [];
            AllId = AssertArr(id);
    
            for(var i = 0;i < AllId.length;++i) {
                var id = ID(AllId[i]);
                if(!id) {
                    continue;
                }
    
                existingClasses = id.className.split(" ");
    
                for(var j = 0;j < existingClasses.length;++j) {
                    if(existingClasses[j] == oldClass) {
                        existingClasses.splice(j , 1);
                    }
                }
    
                id.className = existingClasses.join(" ");
            }
        } ,
        ClassApply = function(className , a) {
            var className = AssertArr(className);
    
            for(var i = 0;i < className.length;++i) {
                var myClass = GetClass(className[i]);
                if(!myClass) {
                    continue;
                }
    
                if(a.trigger) {
                    for(var j = 0;j < myClass.length;++j) {
                        myClass[j][a.trigger] = a.action;
                    }
                }
                else {
                    for(j = 0;j < myClass.length;++j) {
                        a.action.apply(myClass[j]);
                    }
                }
    
            }
        } ,
        Disable = function(element , trigger , replacementFunction) {
            var element = AssertArr(element);
    
            for(var i = 0;i < element.length;++i) {
                var e = ID(element[i]);
                if(!e) {
                    continue;
                }
                if(e["old" + trigger] && e["old" + trigger] != "null") {
                    continue;
                }
                if(typeof e[trigger] === "function") {
                    e["old" + trigger] = e[trigger];
                }
                if(e.href && (trigger == "onclick" || trigger == "ondbclick")) {
                    e.href = "javascript:void(0)";
                }
                if(replacementFunction) {
                    e[trigger] = replacementFunction;
                }
                else {
                    e[trigger] = function() {
                        return false;
                    }
                }
            }
        } ,
        Enable = function(element , trigger) {
            var element = AssertArr(element);
    
            for(var i = 0;i < element.length;++i) {
                var e = ID(element[i]);
                if(!e || !e["old" + trigger]) {
                    continue;
                }
                if(typeof e[trigger] === "function") {
                    e[trigger] = e["old" + trigger];
                    e["old" + trigger] = "null";
                }
            }
    
        } ,
        AssertStyle = function(e , o) {
            var e = AssertArr(e);
    
            for(var i = 0;i < e.length;++i) {
                for(var j in o) {
                    if(!ID(e)) {
                        continue;
                    }
                    CSS(e[i])[j] = o[j];
                }
            }
        } ,
        Center = function() {
            var center = this ,
                X = 
                Y = [] ,
                Align = function() {
                    for(var i = 0;i < X.length;++i) {
                        var xPos = Math.floor(parseInt(ID(X[i]).parentNode.offsetWidth) / 2 - parseInt(ID(X[i]).offsetWidth) / 2);
                        CSS(X[i]).left = xPos > 0 ? xPos + "px" : "0px";
                    }
                    for(var i = 0;i < Y.length;++i) {
                        var yPos = Math.floor(parseInt(ID(Y[i]).parentNode.offsetHeight) / 2 - parseInt(ID(Y[i]).offsetHeight) / 2);
                        CSS(Y[i]).top = yPos > 0 ? yPos + "px" : "0px";
                    }
                };
    
            Loop.call(center , Align);
            center.init();
    
            return{
                "X":function(e) {
                    if(!CSS(e).position) {
                        AssertStyle(e  , {"position":"relative"});
                    }
                    X.push(e);
                } ,
                "Y":function(e) {
                    if(!CSS(e).position) {
                        AssertStyle(e  , {"position":"relative"});
                    }
                    Y.push(e);
                } ,
                "XY":function(e) {
                    if(!CSS(e).position) {
                        AssertStyle(e  , {"position":"relative"});
                    }
                    X.push(e) , Y.push(e);
                }
            }
        }() ,
        Observer = function(ConditionIsTrue , codeToExecute) {
            "Private";var 
                observer = this ,
                ConditionWasMet = false ,
                CheckIfReady = function() {
                    if(ConditionIsTrue()) {
                        if(!ConditionWasMet) {
                            codeToExecute();
                        }
                        ConditionWasMet = true;
                    }
                    else {
                        if(ConditionWasMet) {
                            ConditionWasMet = false;
                        }
                    }
                };
    
            Loop.call(observer , CheckIfReady);
            observer.speed(1);
    
        } ,
        Clone = function(a) {
            if(null == a || "object" != typeof a) {
                return[];
            }
            var c = a.constructor() , b;
            for(b in a) {
                a.hasOwnProperty(b) && (c[b] = a[b]);
            }
            return c;
        } ,
        CloneArray = function(b) {
            for(var c = [] , a = 0;a < b.length;++a) {
                c.push(b[a]);
            }
            return c;
        } ,
        isArray = function(o) {
            return Object.prototype.toString.call(o) === "[object Array]";
        };
        Time["getFromSeconds"] = function(secs) {
            time = [0 , 0 , secs];
            for(var i = 2;i > 0;i--) {
                time[i - 1] = Math.floor(time[i] / 60);
                time[i] = time[i] % 60;
                if(time[i] < 10) {
                    time[i] = "0" + time[i];
                }
            }
            return time.join(":");
        };
        return{
            "HTML":HTML ,
            "CSS":CSS ,
            "ID":ID ,
            "S":AddSpace ,
            "isArray":isArray ,
            "Strip":Strip ,
            "Center":Center ,
            "Clean":Clean ,
            "Time":Time ,
            "GetClass":GetClass ,
            "AddClass":AddClass ,
            "RemoveClass":RemoveClass ,
            "ClassApply":ClassApply ,
            "Disable":Disable ,
            "Enable":Enable ,
            "Clone":Clone ,
            "CloneArray":CloneArray ,
            "Observer":Observer ,
            "Loop":Loop ,
            "$":AssertStyle
        };
    }();
    Last edited by blaze4218; 03-21-2012 at 08:59 PM.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    I put that in
    http://closure-compiler.appspot.com/home
    and got no errors
    (only some warnings about useless code)

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    not errors

    It works fine, no problems that I know of...
    For the first time the compiler has added something to my global scope
    Can someone explain to me why the var ConditionWasMet had to be exported to the global scope?
    I have put much effort into encapsulating my entire library, and don't quite understand why the compiler felt the need to export that variable, something it has never done before.

    My only guess would be that it perceived it to be a global variable, and defined it in the "appropriate" scope... but why would it have that perception? Did I fail to localize the variable?
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by blaze4218 View Post
    I have devised the following constructor based loosely on the observer pattern
    Code:
    Observer = function (ConditionIsTrue , codeToExecute){
                
                var
                    observer = this ,
                    ConditionWasMet = false ,
                    
                    CheckIfReady = function () {
                    
                        if (ConditionIsTrue()) {
                            
                            if(!ConditionWasMet)
                                codeToExecute();
                            
                            ConditionWasMet = true;
                        
                        }
                        else {
                            if (ConditionWasMet) ConditionWasMet = false;
                        }
                    };
                
                Loop.call(observer , CheckIfReady)
                observer.speed(1)
            
            };
    It works fine, no problems that I know of...

    But the most interesting thing happened when I attached it to my lib and ran it through the http://closure-compiler.appspot.com/home compiler
    Code:
    $['Observer'] = function (//...etc...
    For the first time the compiler has added something to my global scope
    Code:
    var i=!1;
    window.$=function(){//...etc...
    ...
    Observer:function(j,k){var h=i;Loop.call(this,function(){j()?(h||k(),h=!0):h&&(h=i)});this.speed(1)}
    }
    })();
    Can someone explain to me why the var ConditionWasMet had to be exported to the global scope?
    Was it already in the global scope? That would confuse me considering the observer can be called from multiple instances without conflict...
    But I don't want any surprise conflicts jumping out at me later, I appreciate the consideration.
    I can't find any of the above
    code in the code below ?



    Quote Originally Posted by blaze4218 View Post
    well...
    the whole code is considerably larger in size,
    the relevant code is here (I tried this and other variations which all met the same fate),
    the actual output of the compiler was in my post, save 1 renamed variable for brevity,
    I have successfully narrowed the generation of the exported global variable to ConditionWasMet via trial and error (removing the lines ConditionWasMet = true; and if (ConditionWasMet) ConditionWasMet = false; removed the global var),
    many contributors here shun posting irrelevant code...

    But if you think it will help
    Code:
    window["BW"] = function() {
        "use strict";
    
        var Loop = function(myFunct) {
            "Private"; var
                loop = this ,
                rate = 1E3 ,
                code = myFunct ,
                paused = false ,
                run = function() {
                    clearTimeout(loop.Cycle);
                    if(code) {
                        if(!paused) {
                            code()
                        }
                        loop.Cycle = setTimeout(run , rate)
                    }
                };
            "Public";
                loop["resume"] = function() {
                    paused = false;
                } ,
                loop["pause"] = function() {
                    paused = true;
                } ,
                loop["die"] = function() {
                    code = false;
                } ,
                loop["speed"] = function(r) {
                    r && typeof r == "number" && r > 0 && (rate = r);
                    return rate;
                } ,
                loop["init"] = function() {
                    if(!loop.Cycle && code) {
                        run();
                    }
                }
        } ,
        ID = function(e) {
            return"object" === typeof e ? e : document.getElementById(e);
        } ,
        CSS = function(e) {
            if(!ID(e)) {
                return false;
            }
            return ID(e).style;
        } ,
        HTML = function(e , s , q) {
            var tmp;
            q ? (tmp = document.createElement("div") , tmp.innerHTML = s , ID(e).appendChild(tmp)) : ID(e).innerHTML = s;
        } ,
        AddSpace = function(b) {
            var s = "";
            for(var i = 0;i < b;++i) {
                s += "&nbsp;"
            }
            return s;
        } ,
        Strip = function(e) {
            ID(e).innerHTML = ID(e).innerHTML.replace(/\s*</gi , "<");
        } ,
        Clean = function() {
            return{
                "Alpha":function(v) {
                    return v.replace(/[^a-zA-Z]/g , "");
                } ,
                "Numeric":function(v) {
                    return v.replace(/[^0-9]/g , "");
                } ,
                "AlphaNumeric":function(v) {
                    return v.replace(/[^0-9a-zA-Z]/g , "");
                } ,
                "Email":function(v) {
                }
            }
        }() ,
        Time = function() {
            return new Date;
        } ,
        AssertArr = function(a) {
            return isArray(a) ? a : [a];
        } ,
        GetClass = function(c) {
            var a = 
                d = 
                b = [];
            a = document.getElementsByTagName("*");
            for(var i = 0;i < a.length;++i) {
                b = a[i].className.split(" ");
                for(var j = 0;j < b.length;++j) {
                    b[j] == c && d.push(a[i]);
                }
            }
            return d;
        } ,
        AddClass = function(id , newClass) {
            var existingClasses = [] ,
                AllId = AssertArr(id);
    
            for(var i = 0;i < AllId.length;++i) {
                var id = ID(AllId[i]);
                existingClasses = id.className.split(" ");
                for(var j = 0;j < existingClasses.length;++j) {
                    if(existingClasses[j] == newClass) {
                        return;
                    }
                }
                existingClasses.push(newClass);
                id.className = existingClasses.join(" ");
            }
        } ,
        RemoveClass = function(id , oldClass) {
            var existingClasses = [];
            AllId = AssertArr(id);
    
            for(var i = 0;i < AllId.length;++i) {
                var id = ID(AllId[i]);
                if(!id) {
                    continue;
                }
    
                existingClasses = id.className.split(" ");
    
                for(var j = 0;j < existingClasses.length;++j) {
                    if(existingClasses[j] == oldClass) {
                        existingClasses.splice(j , 1);
                    }
                }
    
                id.className = existingClasses.join(" ");
            }
        } ,
        ClassApply = function(className , a) {
            var className = AssertArr(className);
    
            for(var i = 0;i < className.length;++i) {
                var myClass = GetClass(className[i]);
                if(!myClass) {
                    continue;
                }
    
                if(a.trigger) {
                    for(var j = 0;j < myClass.length;++j) {
                        myClass[j][a.trigger] = a.action;
                    }
                }
                else {
                    for(j = 0;j < myClass.length;++j) {
                        a.action.apply(myClass[j]);
                    }
                }
    
            }
        } ,
        Disable = function(element , trigger , replacementFunction) {
            var element = AssertArr(element);
    
            for(var i = 0;i < element.length;++i) {
                var e = ID(element[i]);
                if(!e) {
                    continue;
                }
                if(e["old" + trigger] && e["old" + trigger] != "null") {
                    continue;
                }
                if(typeof e[trigger] === "function") {
                    e["old" + trigger] = e[trigger];
                }
                if(e.href && (trigger == "onclick" || trigger == "ondbclick")) {
                    e.href = "javascript:void(0)";
                }
                if(replacementFunction) {
                    e[trigger] = replacementFunction;
                }
                else {
                    e[trigger] = function() {
                        return false;
                    }
                }
            }
        } ,
        Enable = function(element , trigger) {
            var element = AssertArr(element);
    
            for(var i = 0;i < element.length;++i) {
                var e = ID(element[i]);
                if(!e || !e["old" + trigger]) {
                    continue;
                }
                if(typeof e[trigger] === "function") {
                    e[trigger] = e["old" + trigger];
                    e["old" + trigger] = "null";
                }
            }
    
        } ,
        AssertStyle = function(e , o) {
            var e = AssertArr(e);
    
            for(var i = 0;i < e.length;++i) {
                for(var j in o) {
                    if(!ID(e)) {
                        continue;
                    }
                    CSS(e[i])[j] = o[j];
                }
            }
        } ,
        Center = function() {
            var center = this ,
                X = 
                Y = [] ,
                Align = function() {
                    for(var i = 0;i < X.length;++i) {
                        var xPos = Math.floor(parseInt(ID(X[i]).parentNode.offsetWidth) / 2 - parseInt(ID(X[i]).offsetWidth) / 2);
                        CSS(X[i]).left = xPos > 0 ? xPos + "px" : "0px";
                    }
                    for(var i = 0;i < Y.length;++i) {
                        var yPos = Math.floor(parseInt(ID(Y[i]).parentNode.offsetHeight) / 2 - parseInt(ID(Y[i]).offsetHeight) / 2);
                        CSS(Y[i]).top = yPos > 0 ? yPos + "px" : "0px";
                    }
                };
    
            Loop.call(center , Align);
            center.init();
    
            return{
                "X":function(e) {
                    if(!CSS(e).position) {
                        AssertStyle(e  , {"position":"relative"});
                    }
                    X.push(e);
                } ,
                "Y":function(e) {
                    if(!CSS(e).position) {
                        AssertStyle(e  , {"position":"relative"});
                    }
                    Y.push(e);
                } ,
                "XY":function(e) {
                    if(!CSS(e).position) {
                        AssertStyle(e  , {"position":"relative"});
                    }
                    X.push(e) , Y.push(e);
                }
            }
        }() ,
        Observer = function(ConditionIsTrue , codeToExecute) {
            "Private";var 
                observer = this ,
                ConditionWasMet = false ,
                CheckIfReady = function() {
                    if(ConditionIsTrue()) {
                        if(!ConditionWasMet) {
                            codeToExecute();
                        }
                        ConditionWasMet = true;
                    }
                    else {
                        if(ConditionWasMet) {
                            ConditionWasMet = false;
                        }
                    }
                };
    
            Loop.call(observer , CheckIfReady);
            observer.speed(1);
    
        } ,
        Clone = function(a) {
            if(null == a || "object" != typeof a) {
                return[];
            }
            var c = a.constructor() , b;
            for(b in a) {
                a.hasOwnProperty(b) && (c[b] = a[b]);
            }
            return c;
        } ,
        CloneArray = function(b) {
            for(var c = [] , a = 0;a < b.length;++a) {
                c.push(b[a]);
            }
            return c;
        } ,
        isArray = function(o) {
            return Object.prototype.toString.call(o) === "[object Array]";
        };
        Time["getFromSeconds"] = function(secs) {
            time = [0 , 0 , secs];
            for(var i = 2;i > 0;i--) {
                time[i - 1] = Math.floor(time[i] / 60);
                time[i] = time[i] % 60;
                if(time[i] < 10) {
                    time[i] = "0" + time[i];
                }
            }
            return time.join(":");
        };
        return{
            "HTML":HTML ,
            "CSS":CSS ,
            "ID":ID ,
            "S":AddSpace ,
            "isArray":isArray ,
            "Strip":Strip ,
            "Center":Center ,
            "Clean":Clean ,
            "Time":Time ,
            "GetClass":GetClass ,
            "AddClass":AddClass ,
            "RemoveClass":RemoveClass ,
            "ClassApply":ClassApply ,
            "Disable":Disable ,
            "Enable":Enable ,
            "Clone":Clone ,
            "CloneArray":CloneArray ,
            "Observer":Observer ,
            "Loop":Loop ,
            "$":AssertStyle
        };
    }();
    Maybe post the code that
    causes you concern.

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    well, now we are just going in circles...
    Code:
     post the code that
    causes you concern.
    the code that concerns me is in post #1, as is the out put that I question...

    I have edited post #2 highlighting the code from post #1 in blue, the lines that cause the mysterious var j=!1; are highlighted in red. Commenting the red lines will result in a fully encapsulated BW object to which I am accustomed.

    To satisfy your request even further I have tried to recreate the output with only the relevant crossection of my lib
    Code:
    window["BW"] = function() {
      var Loop = function(myFunct) {
        "Private";
        var loop = this, rate = 1E3, code = myFunct, paused = false, run = function() {
          clearTimeout(loop.Cycle);
          if(code) {
            if(!paused) {
              code()
            }
            loop.Cycle = setTimeout(run, rate)
          }
        };
        "Public";
        loop["resume"] = function() {
          paused = false
        }, loop["pause"] = function() {
          paused = true
        }, loop["die"] = function() {
          code = false
        }, loop["speed"] = function(r) {
          r && typeof r == "number" && r > 0 && (rate = r);
          return rate
        }, loop["init"] = function() {
          if(!loop.Cycle && code) {
            run()
          }
        }
      }, Observer = function(ConditionIsTrue, codeToExecute) {
        "Private";
        var observer = this, ConditionWasMet = false, CheckIfReady = function() {
          if(ConditionIsTrue()) {
            if(!ConditionWasMet) {
              codeToExecute()
            }
            ConditionWasMet = true
          }else {
            if(ConditionWasMet) {
              ConditionWasMet = false
            }
          }
        };
        Loop.call(observer, CheckIfReady);
        observer.speed(1)
      };
      return{"Loop":Loop, "Observer":Observer}
    }();
    and that did not reproduce the bizarre line var j=!1;

    I think the question is simpler than my first post has led you to believe...

    Q:
    Is there anything in my Observer object that exists in the global scope?
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by blaze4218 View Post

    I think the question is simpler than my first post has led you to believe...

    Q:
    Is there anything in my Observer object that exists in the global scope?
    Code:
    <script>
    
    function Loop(){CheckIfReady()}
    function ConditionIsTrue(){return true}
    function codeToExecute(){return true}
    Observer = function (ConditionIsTrue , codeToExecute){
                
                var
                    observer = this ,
                    ConditionWasMet = false 
                    
                   CheckIfReady = function () {
                    
                        if (ConditionIsTrue()) {
                            
                            if(!ConditionWasMet)
                                codeToExecute();
                            
                            ConditionWasMet = true;
                        
                        }
                        else {
                            if (ConditionWasMet) ConditionWasMet = false;
                        }
                    };      
    
    
          
                Loop.call(observer , CheckIfReady)
                //observer.speed(1)
            
            };
    a = new Observer(ConditionIsTrue , codeToExecute);
    
    alert(CheckIfReady)
    
    
    </script>
    Last edited by DaveyErwin; 03-21-2012 at 11:52 PM.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    this===window by default...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #10
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by rnd me View Post
    this===window by default...
    Code:
    <script>
    function Loop(){CheckIfReady()}
    function ConditionIsTruefn(){return true}
    function codeToExecutefn(){return true}
    Observer = function (ConditionIsTrue , codeToExecute){            
                var
                    observer = this ,
                    ConditionWasMet = false 
                    
                   CheckIfReady = function () {
                    
                        if (ConditionIsTrue()) {
                            
                            if(!ConditionWasMet)
                                codeToExecute();
                            
                            ConditionWasMet = true;
                        
                        }
                        else {
                            if (ConditionWasMet) ConditionWasMet = false;
                        }
                    };       
                Loop.call(observer , CheckIfReady)
                //observer.speed(1)
            alert(window == observer)// alerts false
            };
    a = new Observer(ConditionIsTruefn , codeToExecutefn);
    </script>

  • #11
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    Code:
    Observer = function (ConditionIsTrue , codeToExecute){
                
                var
                    observer = this ,
                    ConditionWasMet = false 
                    
                   CheckIfReady = function () {
                    
                        if (ConditionIsTrue()) {
                            
                            if(!ConditionWasMet)
                                codeToExecute();
                            
                            ConditionWasMet = true;
                        
                        }
                        else {
                            if (ConditionWasMet) ConditionWasMet = false;
                        }
                    };      
    
    
          
                Loop.call(observer , CheckIfReady)
                //observer.speed(1)
            
            };
    a = new Observer(ConditionIsTrue , codeToExecute);
    
    alert(CheckIfReady)


    You don't initialise CheckIfReady in the local scope (ie: in the scope of Observer). Now, you'd have through this would have thrown an undefined error, but it looks like your compiler has gotten around that by initialising it in the global scope...

    In real terms:

    Code:
     CheckIfReady = function () {
                    
                        if (ConditionIsTrue()) {
                            
                            if(!ConditionWasMet)
                                codeToExecute();
                            
                            ConditionWasMet = true;
                        
                        }
                        else {
                            if (ConditionWasMet) ConditionWasMet = false;
                        }
                    };
    Should be:

    Code:
     var CheckIfReady = function () {
                    
                        if (ConditionIsTrue()) {
                            
                            if(!ConditionWasMet)
                                codeToExecute();
                            
                            ConditionWasMet = true;
                        
                        }
                        else {
                            if (ConditionWasMet) ConditionWasMet = false;
                        }
                    };

    Is that the issue? Or am I reading the above all wrong?

    M
    Last edited by Mikebert4; 03-22-2012 at 03:09 PM. Reason: Missed a CODE tag :(

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Quote Originally Posted by DaveyErwin View Post
    Code:
    <script>
    
    function Loop(){CheckIfReady()}
    function ConditionIsTrue(){return true}
    function codeToExecute(){return true}
    Observer = function (ConditionIsTrue , codeToExecute){
                
                var
                    observer = this ,
                    ConditionWasMet = false ,   // <-Your missing a very important comma...
                    
                   CheckIfReady = function () {
                    
                        if (ConditionIsTrue()) {
                            
                            if(!ConditionWasMet)
                                codeToExecute();
                            
                            ConditionWasMet = true;
                        
                        }
                        else {
                            if (ConditionWasMet) ConditionWasMet = false;
                        }
                    };      
    
    
          
                Loop.call(observer , CheckIfReady)
                //observer.speed(1)
            
            };
    a = new Observer(ConditionIsTrue , codeToExecute);
    
    alert(CheckIfReady)
    
    
    </script>
    your alert() (and Loop function) will fail. That comma that you removed ended the series of local variable declarations...
    I do get your point that I should test ConditionWasMet in the global scope to see if it exists there. I used the same alert method that you used in your test, and it was undefined as it should be! I am stumped as to why the compiler would have initialized this variable in the global scope!

    @rnd_me by default yes, but it changes with scope. (that's the main reason i alias it to a variable first off so that I don't loose track of the scope of the object I am referencing. Also the variable reference will be rewritten as a single character by closure-compiler, allowing for greater minification when called more than 3 times for a single object)

    @Mikebert4 that's a good point, but it's not in any of the code I posted. Only in DaveyErwins and yours.
    Also, it is ConditionWasMet that has been moved to the global scope
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #13
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    Ahh, my apologies for misreading...

    In which case, I'm as stumped as you are - I can't see why it would feel the need to move it to global.


  •  

    Posting Permissions

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