[JS] To Read: "The Essentials of Writing High Quality JavaScript"

Received through The Code Project Daily News:
For all just-starting Javascript writers as well as for the seasoned ones -- and the ones that think they know a trick or two : an excerpt by Stoyan Stefanov in honor of his new O'Reilly title “JavaScript Patterns”.
A single page article, but it's a long page, and describes enough super-duper coding practice top tips to make your mouth water for the full deal.
http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-j avascript/

George,
It looks like this:
First Script:
(function() {
//snip
for (var j = 0; supportScripts.length > j; j++) {
     if (myTPS.timingInfo) {
          var supportStart = new Date();
     app.doScript(supportScripts[j]);
     if (myTPS.timingInfo) {
          var supportEnd = new Date();
          myTPS.log(supportScripts[j].name + " duration: " + (supportEnd - supportStart) + " ms.", interactive);
} // end for
Support Script:
(function() {
     // include the event services object and functions
     #include 'LC250III:ScriptSources:TPSevServe.jsx'
     // ++++++++++++++++ Main code function follows +++++++++++++++++++++++
     function performFunction(aDoc, interactive, eventId, myTPS) {
          // Here's where the actual code that does something useful goes
          // In here, I had a loop where I used j but forgot to declare it with var
          // Like this:
              for (j = 0, 5 > j, j++) {
                    // whatever
     } // end performFunction
     // ++++++++++++++++ Kick-off Code starts here +++++++++++++++++++++++++
     // If the job.id scriptArg is defined, we're running under Publish
     var interactive = !(app.scriptArgs.isDefined("job.id"));
     // make our working object
     var myTPS = new TPSevServ();
     // locate the "active" document
     if (app.scriptArgs.isDefined("job.activeDocument")) {
          var aDoc = app.documents.item(app.scriptArgs.get("job.activeDocument"));
     } else {
          var aDoc = myTPS.findLiveDoc();
          if (aDoc === null) {
               myTPS.log("Couldn't locate active document.", interactive);
               return;
     var eventId = app.scriptArgs.get("job.eventId");
     try {
          performFunction(aDoc, interactive, eventId, myTPS);
     } catch(e) {
          myTPS.log("Error " + e.number + ": " + e.message + " at line: " + e.line, interactive);
It turned out that by failing to declare that deeply internal loop variable as a local (I think it was even deeper than I'm showing here in that it was inside a called function), I was accidentally using the same j as in the calling script. As a result, when the calling script tried to post the duration to the log, it got an error because the value of j was wrong for picking up the supportScript's name. The error was a cryptic "[21]".
At least next time I'll know what it means.
But it has shaken my confidence in the value of anonymous functions. I guess the point is that names inside an anonymous function don't leak upwards into the global space, but they can flow downwards.
Dave

Similar Messages

  • Reading the Blob and writing it to an external file in an xml tree format

    Hi,
    We have a table by name clarity_response_log and content of the column(Response_file) is BLOB and we have xml file or xml content in that column. Most probably the column or table may be having more than 5 records and hence we need to read the corresponding blob content and write to an external file.
    CREATE TABLE CLARITY_RESPONSE_LOG
      REQUEST_CODE   NUMBER,
      RESPONSE_FILE  BLOB,
      DATE_CRATED    DATE                           NOT NULL,
      CREATED_BY     NUMBER                         NOT NULL,
      UPDATED_BY     NUMBER                         DEFAULT 1,
      DATE_UPDATED   VARCHAR2(20 BYTE)              DEFAULT SYSDATE
    )The xml content in the insert statement is very small because of some reason and cannot be made public and indeed we have a very big xml file stored in the BLOB column or Response_File column
    Insert into CLARITY_RESPONSE_LOG
       (REQUEST_CODE, RESPONSE_FILE, DATE_CRATED, CREATED_BY, UPDATED_BY, DATE_UPDATED)
    Values
       (5, '<?xml version="1.0" encoding="UTF-8"?><xml-response><phone-number>1212121212</tracking-number></xml-response>', TO_DATE('09/23/2010 09:01:34', 'MM/DD/YYYY HH24:MI:SS'), 1, 1, '23-SEP-10');
    Insert into CLARITY_RESPONSE_LOG
       (REQUEST_CODE, RESPONSE_FILE, DATE_CRATED, CREATED_BY, UPDATED_BY, DATE_UPDATED)
    Values
       (6, '<?xml version="1.0" encoding="UTF-8"?><xml-response><phone-number>1212121212</tracking-number></xml-response>', TO_DATE('09/23/2010 09:01:34', 'MM/DD/YYYY HH24:MI:SS'), 1, 1, '23-SEP-10');
    Insert into CLARITY_RESPONSE_LOG
       (REQUEST_CODE, RESPONSE_FILE, DATE_CRATED, CREATED_BY, UPDATED_BY, DATE_UPDATED)
    Values
       (7, '<?xml version="1.0" encoding="UTF-8"?><xml-response><phone-number>1212121212</tracking-number></xml-response>', TO_DATE('09/23/2010 09:01:34', 'MM/DD/YYYY HH24:MI:SS'), 1, 1, '23-SEP-10');
    Insert into CLARITY_RESPONSE_LOG
       (REQUEST_CODE, RESPONSE_FILE, DATE_CRATED, CREATED_BY, UPDATED_BY, DATE_UPDATED)
    Values
       (8, '<?xml version="1.0" encoding="UTF-8"?><xml-response><phone-number>1212121212</tracking-number></xml-response>', TO_DATE('09/23/2010 09:01:34', 'MM/DD/YYYY HH24:MI:SS'), 1, 1, '23-SEP-10');
    Insert into CLARITY_RESPONSE_LOG
       (REQUEST_CODE, RESPONSE_FILE, DATE_CRATED, CREATED_BY, UPDATED_BY, DATE_UPDATED)
    Values
       (9, '<?xml version="1.0" encoding="UTF-8"?><xml-response><phone-number>1212121212</tracking-number></xml-response>', TO_DATE('09/23/2010 09:01:34', 'MM/DD/YYYY HH24:MI:SS'), 1, 1, '23-SEP-10');THe corresponding proc for reading the data and writing the data to an external file goes something like this
    SET serveroutput ON
    DECLARE
       vstart     NUMBER             := 1;
       bytelen    NUMBER             := 32000;
       len        NUMBER;
       my_vr      RAW (32000);
       x          NUMBER;
       l_output   UTL_FILE.FILE_TYPE;
    BEGIN
    -- define output directory
       l_output :=
          UTL_FILE.FOPEN ('CWFSTORE_RESPONCE_XML', 'extract500.txt', 'wb', 32760);
       vstart := 1;
       bytelen := 32000;
    ---get the Blob locator
       FOR rec IN (SELECT response_file vblob
                     FROM clarity_response_log
                    WHERE TRUNC (date_crated) = TRUNC (SYSDATE - 1))
       LOOP
    --get length of the blob
    len := DBMS_LOB.getlength (rec.vblob);
          DBMS_OUTPUT.PUT_LINE (len);
          x := len;
    ---- If small enough for a single write
    IF len < 32760
          THEN
             UTL_FILE.put_raw (l_output, rec.vblob);
             UTL_FILE.FFLUSH (l_output);
          ELSE  
    -------- write in pieces
             vstart := 1;
             WHILE vstart < len AND bytelen > 0
             LOOP
                DBMS_LOB.READ (rec.vblob, bytelen, vstart, my_vr);
                UTL_FILE.put_raw (l_output, my_vr);
                UTL_FILE.FFLUSH (l_output);
    ---------------- set the start position for the next cut
                vstart := vstart + bytelen;
    ---------- set the end position if less than 32000 bytes
                x := x - bytelen;
                IF x < 32000
                THEN
                   bytelen := x;
                END IF;
                UTL_FILE.NEW_LINE (l_output);
             END LOOP;
    ----------------- --- UTL_FILE.NEW_LINE(l_output);
          END IF;
       END LOOP;
       UTL_FILE.FCLOSE (l_output);
    END;The above code works well and all the records or xml contents are being written simultaneously adjacent to each other but we each records must be written to a new line or there must be a line gap or a blank line between any two records
    the code which I get is as follow all all xml data comes on a single line
    <?xml version="1.0" encoding="ISO-8859-1"?><emp><empno>7369</empno><ename>James</ename><job>Manager</job><salary>1000</salary></emp><?xml version="1.0" encoding="ISO-8859-1"?><emp><empno>7370</empno><ename>charles</ename><job>President</job><salary>500</salary></emp>But the code written to an external file has to be something like this.
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <emp>
      <empno>7369</empno>
      <ename>James</ename>
      <job>Manager</job>
      <salary>1000</salary>
    </emp>
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <emp>
    <empno>7370</empno>
    <ename>charles</ename>
    <job>President</job>
    <salary>500</salary>
    </emp>Please advice

    What was wrong with the previous answers given on your other thread:
    Export Blob data to text file(-29285-ORA-29285: file write error)
    If there's a continuing issue, stay with the same thread, don't just ask the same question again and again, it's really Pi**es people off and causes confusion as not everyone will be familiar with what answers you've already had. You're just wasting people's time by doing that.
    As already mentioned before, convert your BLOB to a CLOB and then to XMLTYPE where it can be treated as XML and written out to file in a variety of ways including the way I showed you on the other thread.
    You really seem to be struggling to get the worst possible way to work.

  • CS6 InDesign - Default display is high-quality. Four placed images are Illustrator files. Three images look the way they should - high-quality. One image looks pixelated. Why?

    InDesign CS6 - Default display is high-quality. All four placed images are Illustrator files. Three images look the way they should - high-quality. One image looks pixelated. Why?

    Not sure if this will help, but what is your inDesign file's "Transparency Blend Space" set to? It's under the edit menu.
    Also, when you are placing your images, are you selecting "Show Import Options" and making any selections there?

  • Purchasing the higher-quality songs vs. regular quality

    My computer just asked me if I wanted to purchase a song in the higher quality and told me where I could disable that feature, but it didn't tell me that the songs would change from $0.99 to $1.39 and now that I want to go back so that it just offers them in standard quality, I can't remember what the name of that higher quality thing was, and I've been looking through my menu bar and can't see anything anywhere about taking it off! What the heck is it again and where do I find it in my menu bar?

    Log in to the Store. Click "Account" in the Quick Links area to the right of your Screen.
    When you get through to your Apple Account Information page, up the top you'll be seeing "iTunes Plus: Enabled".
    Click the "Manage iTunes Plus" button next to that.
    Uncheck "Always show me iTunes Plus music and music when available".
    Click "Save Changes".
    The Apple Account Information will now be saying "iTunes Plus: Disabled".
    You'll be back to just seeing the $0.99 selections again.

  • The deinterlace and high quality settings

    Hi !
    I've seen the options "deinterlace" and "high quality" for the video track in the sequence properties from "window" of Quicktime.
    Does anyone know what they do exactly ?
    Thank you
    Olivier
    G4, 1.42 gHz   Mac OS X (10.4.9)  

    Open Automator.
    Click once on the word QuickTime in the "Library" (left most pane).
    Drag the "Set Movie Playback Properties" (Action pane) to the right side part of the window.
    Set your parameters and save the workflow and an Application.
    Drag your QuickTime files onto this Application (one at a time or as a group) and they will be converted.

  • No high quality previews in Bridge under Version Cue

    I posted this in the 'Bridge' forum but didn't get any responses... thinking maybe someone here might be able to help?
    I've started to work with Version Cue, which seems like a useful idea.
    However, within Bridge, when I preview a PSD file it shows a pixelated, low-res version. I have set the preferences to display "high quality previews" and purged the cache, restarted, etc. The previews for non-Version Cue files are fine.
    For Illustrator files under Version Cue, the previews stay small - i.e. the same size as the thumbnails.
    I really need to be able to view HQ previews in order to continue using Version Cue - anyone know how to make that happen?

    I was quite calm.
    I've previously posted messages here and always had them answered within an hour or so, so that's why I wasn't sure if I'd posted in the correct forum. It's not that I expect "you" to answer me more quickly. Obviously this question falls between several different products, so I thought maybe I should have found a 'Version Cue' forum or something.
    Sorry, I should have provided version details:
    Bridge version 2.1.1.9
    Version Cue version 3.1.0
    Running on OSX 10.5.4
    It seems to me that one of the main uses of Bridge is to be able to use high quality previews to look through alternate designs. So if this doesn't work with Version Cue, which is all about controlling different versions of designs, that seems unfortunate.

  • FILE PLACE images are SO blurry in photoshop CC-.. (these are high quality photos) any tips?

    I am deleting backgrounds using eraser > saving > then combining these images into a collage on a new blank document. When I file place the tif's are SO BLURRY. I always do my work like this, but just updated my PS. File, Place is now File, Place Embedded or Place Linked. Let me know if y'all have any fixes.

    OK you noted the image files are high quality photos what can you tell us about the document they are being placed into? What is your Photoshop preference for default interpolation set to.?

  • InDesign CS2 High Quality Problem

    When using CS2 inDesign the documents changed to High Quality don't stay that way.  While the file is open, the document will revert to a lesser quality and remain that way even when file is closed, re-opened and high quality is selected again.  I am familar with the file opening in a lesser quality and then changed back to high quality for viewing, this is not the problem.  Thank you  

    You might be inadvertently hitting the keyboard shortcut to change display settings.
    It's usually a system problem otherwise. You could try letting windows manage your Virtual Memory and Paging Files. Take screen shots before you change any settings, in case you need to change it back.

  • How can I get a higher quality export from Imovie - QT & iDVD came out bad

    The playback on my imovie came out corrupted but it transferred ok to Quick Time and I also burned a DVD but the quality is not very good. Any suggestions on how to get a better version. Please keep in mind that the imovie file is huge - 29Gig and the QT file compressed it down to 350Megs. The whole movie is running around 43 minutes. Any suggesstions are appreciated.
    iMac   Mac OS X (10.4.6)  

    sorry, dogma, but you compressed your project by a
    factor of 1:100, and complain quality... ? did you
    use a preset of iM, or did you select any "Expert
    Options"?
    and hopefully, you didn't try to use that crunched
    file for usage in iDVD??
    store/save you project in your Movies Folder, launch
    iDVD, select from the media bin your project - done.
    last questions: is your project set as "mpeg4" or
    "dv/NTSC"; did you import from miniDV (what iM is
    made for...) or did you use any.. other formats?
    happy holidays
    Hi - Yep I did use the crunched version for the dvd and have tossed it. I am now transferring a new version from imovie to QT under the pre set of high quality. Since this is the first time I have done a movie I am not sure what settings I would use under expert options - suggestions? I would love to tranfer the imovie right to idvd but at the moment I only have 8G left on my Mac so this would likely be a problem. The movie was exported from a mini DV.

  • Will ipad allow higher quality video streaming over 4G?

    Will there be improved video quality on youtube and other video streaming on 4G compared to existing 3G connctions?  The ipad appears to dumb down video streaming in 3G compared to streaming the same thing on wifi.  It is often highly pixelated and blocky,   Will the new ipad allow higher quality video streaming over 4G, i.e. the same quality as wi-fi?

    The YouTube app is made by Google. Any suggestions you have about that app should be directed to them.

  • Sequence preset H.264 Extreme High Quality (XHQ) 50Mbps for JVC GY-HM 850e

    Hi there,
    I'm wondering if there is ( or coming soon)  a sequence preset for the new JVC GY-HM850e for editing in the H.264 Extreme High Quality (XHQ) 50Mbps mode.
    I'm using Adobe premiere CS 6
    Regards,
    Martin Beeren

    It's not that CS6 isn't 'supported'.  You can still get help for it, it will still receive bug fixes and whatever updates are needed to work with new Operating Systems.  But CC is the latest version, and it's pretty common practice for companies to put new features only in the newest version.
    It's common for hardware, too.  How often do you see the dealer adding new features to the car you bought last year?  Normally you need to buy this year's model to get the new features.  The only difference is that you can no longer buy, you have to lease.  Some don't like that and will do without the new features.  But the warranty on last year's model is still good.
    Once again, you can get the needed sequence by right clicking on a clip in the bin and selecting New Sequence from Clip.

  • Models with high quality Bluetooth music streaming

    I was planning on buying an e70 with ck20w for bluetooth music streaming but i found a thread on this forum mentioning that E70 streaming has poor quality.
    Can you inform me what models have the capability of stereo high quality bluetooth streaming?

    Have you tried deleting the device, setting the volume high on the phone and pairing again?

  • IPhone 1.0 higher quality than 2.0?

    From the way it sounds, the yellow tinted screen and the plastic case make me think the 1.0 has higher quality parts. Anyone care to comment?

    First off, I totally sympathize with your problem. However 20% is implausible and unless you've asked every iPhone owner, you have no way of knowing how many people this affects.
    That said, this is a support forum, and it seems that a lot of people have tried to offer you....support. If you're unwilling to try what they suggest, then I don't know what you expect. Don't complain about something if you're unwilling to try to find a solution. Please don't think I'm suggesting fault, but it's quite possibly a configuation problem, and you might want to take the phone to a store and let them work on it with you.
    Apple does not do anything offically through these boards, so you *will not* get an answer from Apple here. All you will get are Apple experts, an maybe an employee or two (working unoffically of course) trying to help you. It's up to you if you want to accept it or not.
    Good luck

  • Bridge only shows "high quality thumbnails" even if i have "quick thumbs" selected under preferences

    I have tried using the options "Convert To High Quality When Previewed" and "quick thumbnails" under preferences, and closed and restarted bridge after changing each option, but to no avail.
    bridge only shows high quality thumbs even with other options selected.
    any ideas?

    After the thumbnails have been created using the option
    i Convert To High Quality When Previewed
    then reverting back to the option
    i Quick Thumbnails
    won't "downsize" the cached thumbnails; the high-quality thumbnails will still show. To see low-quality (quick) thumbnails, turn to another folder with images with no thumbnails yet created, or purge the thumbnail cache. Simply closing and restarting won't purge the cache.
    -- Olaf

  • Adding high quality photos to iWEB 08

    Hi folks,
    If I add high quality photos to an iWEB site, will it automatically compress them for the web? Is there any control over the quality?

    Where are you adding them to? If it's to a page rather than a photo page or album resize them to the size you want them to be when in the page, add them to the page, go to the Inspector/Metrics pane and click on Original size button. Also go to the iWeb preferences beforehand and uncheck the option to Optimize images on import.
    If it's photo or album pages again turn off the optimize preferences. That's about all you can do without using different presentations options, like SimpleViewer or Flash Album Export both of which are iPhoto plugins. Those will resize the photos but you can go in and replace the files will higher quality files of the same size.
    This is all predicated on the premise that by higher quality you mean less jpg compression and not larger image size.
    OT

Maybe you are looking for

  • Standard cost update for configurable material- CU50

    Hi, I am trying to update standard cost for a configurable material using CU50 with reference to the sales order. System gives a warning message :- Configuration of raw material still not included in price determination Message no. CK784 Also the sav

  • READ THIS If you're suffering freezing issues to your SL series ThinkPad

    I've owned a ThinkPad SL510 since 2009, been rather happy until I installed the Windows 7 Professional upgrade disc onto it. After using Word 2007 on it for a while, it hanged completely. BIOS and swapping the RAM around fixed it temporarily but it c

  • The same code behaving different in two files.

    The same code behaving different in two files. in pro*c file it is returning rows in sql file it is not returning any rows. please suggest me. regards, prasad.

  • Java Module for changing the file's name

    Hi all I m developing a java module for altering a file's name. I m trying to append system date and time to the file name and send it back. I was successful in retrieving the file's name, appending date n time in it but now I want to send it back. H

  • How to determe Streams tcp/ip network traffic between database nodes.

    We are needing to determine network bandwidth requirements for optimizing streams replication performance between database instances that are NOT located within the same data center. How can I determine the rough # of bits per sec of tcp/ip traffic g