17 Aug 2009

JavaScript __LINE__ and “this” Substitutes

Leaving a longer explanation to aside, the following code demonstrates, how to overcome the absence of the __LINE__ and the proper semantics of the “this” in the JavaScript.

function niceclass(){
    var self_public_=this; // Yes, that's the first "innovation".

    this.a_public_field='whatever';
    function a_private_method(){
        document.write('Public fields value is:'+self_public_.a_public_field);
    }

    this.a_public_method=function(){
        try{
            a_private_method();
        } catch (err){
            // If all of the methods have try-catch-s with
            // Globally Unique Identifiers Copy/Pasted from
            // http://www.guidgenerator.com/ , then
            // it's possible to find out the faulty region even
            // when the JavaScript does not have the __LINE__ support.
            throw "GUID== ba6e3063-9438-42a4-a041-e8fd635fb4c5"+
            "\nerr=="+err+"\n------------------------\n";
        } // catch
    } // this.a_public_method
} // niceclass

window.onload=function(){
    var v1=new niceclass();
    v1.a_public_method();
    v1.a_public_field='Another value';
    v1.a_public_method();
} // window.onload

For the sake of correctness, I have to give credit to the http://www.quirksmode.org/js/this.html and the
http://jspro.org/. Of course those were not the only sources, but as of August 2009 I found them to be the best that I could find.

++++++++++++++++++++++

Update at 2013_04_27:
Tools for debugging and editing GUID-marked JavaScript reside within the BSD-licensed GUID_trace package, which is part of the mmmv_devel_tools package.

The GUID_trace package components assume that GUID-s are surrounded by either double quotes or single quotes, like
 
GUID=="ba6e3063-9438-42a4-a041-e8fd635fb4c5" 
GUID=='ba6e3063-9438-42a4-a041-e8fd635fb4c5'


No comments: