🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #1878

Re: pls explain abt prototype

#1878
From: Harbs
Date: Wed, 21 Jan 2009 19:56
45 lines
2373 bytes
One simple example where prototyping could be very bad:

function1();
function function1(){
    Array.prototype.getItemIndex = function (item){
        for(var i=0;i<this.length;i++){
            if(item==this[i]){return i}
            }
        return undefined;
        }
    var array = [1,3,5,2,4,6];
    DoSomething();
    if(array.getItemIndex(10)==undefined){
        alert("undefined");//do something
        }
    else{alert("defined")}//do something else
    }
function DoSomething(){
    Array.prototype.getItemIndex = function (item){
        for(var i=0;i<this.length;i++){
            if(item==this[i]){return i}
            }
        return -1;
        }
    //do some stuff...
    }

Now, the creator of function1 would expect his script to work properly 
-- and it will as long as he doesn't include DoSomething() in his 
script. Once he includes DoSomething() in his script, the public Array 
object has been changed to conflict with his script. The public objects 
are outside of the scope of namespaces. Both of these prototype methods 
are perfectly legitimate, but will conflict with each other, and you'll 
have a **very hard time** tracking down the problem!!!

This kind of thing can very easily happen with nested scripts especially 
when event handlers are invloved. The DoSomething() function might not 
even be in your script, and you might not know of its existence... Of 
course using private scripting engines avoids these issues when used 
carefully, but I'd rather keep everything in its own namespace and safe 
from any chance of conflicts...

-- 
Harbs
http://www.in-tools.com
Technical Details
Message-ID: <C0F57B8152FB30A716053CCEF638D5C7@in.webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news2.glorb.com!news.glorb.com!newshub.sdsu.edu!webcrossing
References: <59b78cbc.-1@webcrossing.la2eafNXanI> <59b78cbc.6@webcrossing.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,ba3fc2ccca9a708d
X-Google-Attributes: gidf939a,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: Harbs
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news2.glorb.com!news.glorb.com!newshub.sdsu.edu!webcrossing
Newsgroups: adobe.scripting.indesign
Subject: Re: pls explain abt prototype
Message-ID: <C0F57B8152FB30A716053CCEF638D5C7@in.webcrossing.la2eafNXanI>
Date: Wed, 21 Jan 2009 11:56:15 -0800
References: <59b78cbc.-1@webcrossing.la2eafNXanI> <59b78cbc.6@webcrossing.la2eafNXanI>
Lines: 51
User-Agent: Thunderbird 2.0.0.14 (Macintosh/20080421)
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit