Article on DontEnum and OOJS

Discussion in 'Javascript' started by dhtmlkitchen@gmail.com, Oct 19, 2007.

  1. Guest

    I have an article online, but it's not officially linked yet. This
    will be the first public link.

    I've found tons of errors myself, and there's probably plenty more --
    I just didn't find them yet.

    I'd really like to get some review on this. Both technical and
    editorial, but mostly technical.

    The code examples are all plain green. I'll beautify them later (i do
    this in my editor w/a regexp find/rep)

    http://dhtmlkitchen.com/learn/js/enumeration/
    =========================++++++++++++++++++++
    +==========================

    Enumeration and Object Oriented JavaScript

    Who is this Article For?
    This article is primarily intended for JavaScript library authors,
    language implementors, language contributors.

    Anyone who reads this article thoroughly will gain a deeper
    understanding of how enumeration works in JavaScript, and how to deal
    with language complications and cross browser issues.

    For the TG1 group (who is writing ES4), my recommended changes from
    ES3 to are notated as struck out and highlighted. My recommendations
    reflect what I think should change from ECMAScript rev 3 to ECMAScript
    rev 4.
    Synopsis

    This article is about the for in statement, how it is broken in IE,
    and how to work around the problem.

    Prototype Chain
    The for in loop works with the prototype chain. In order to understand
    how the for in loop really works, it is necessary to thoroughly
    understand the prototype chain.

    Constructor chaining affects the prototype chain, which affects
    enumeration.
    Borrowing Properties

    Borrowing properties is taking properties of a supplier object and
    using those properties as properties to receiver object. This is
    usually done using a for in loop. Some examples demonsotrate
    complications with the prototype chain and the problems caused by an
    IE bug that I call the JScript DontEnum bug.

    Borrowing properties is related to constructor chaining. Both can be
    used as a solution to the problem of adding new behavior to a class.
    The two are used sometimes interchangeably, and sometimes together.

    Examples
    I have included some real world code (YUI) and some examples of my
    own. Most of the examples are abbreviated and to the point. The goal
    is to explain JavaScript and programming techniques, which can be used
    to create a real program. The examples display an understanding how
    the prototype chain and enumeration come into play in real-world code.

    This article is quite long. The top level sections should be read all
    the way through, with the exception of the explanation of Creating a
    Subclass, and Borrow methods using an Array. These sections don't have
    any dependencies.

    You can save paper by printing at a smaller scale (60%), and double
    sided (if your printer supports that). Or, you can just read it on
    your computer (I bet you knew that).

    Tests
    There are many live tests that you can and should run. The tests use
    eval to run the textContent of the pre element in each example.

    The tests show three things:

    * How JavaScript works
    * Browser bugs
    * Issues with JavaScript

    If a test does not have a browser table, it is because the test is
    known to pass in every browser. If you encounter such test does not
    pass, please email me.

    If there are any false statements, or if you get a different result
    than what is listed in a browser table, please send an email to me.

    Browsers tested:
    * Internet Explorer 7 (Windows)
    * Mozilla Firefox 2 (Mac, Windows)
    * Opera 9.2 (Mac, Windows)
    * Safari 2 (Mac)
    * Safari 3 (Windows, Webkit on Mac)

    Many of the examples use String.nl

    String.nl = /*@cc_on!@*/false ? "\r\n" : "\n";

    Internet Explorer handles whitespace in unexpected ways. This caused
    several problems with the examples in this article that use innerText
    and innerHTML with the pre element (other elements could not be
    supported). The odd line of code above is a workaround.

    Each test is inside a pre. The test code is run in a closure to keep
    the global namespace clean.

    (function(){ // Closed testF scope.
    // Example test code
    })();

    There is a small templating system that performs evaluation on
    supplanted code marked by ${/*code*/}. This was necessary to
    facilitate white box testing This is viewable in the source of this
    document. The templating system uses a setInnerHTML function to deal
    with Internet Explorer's whitespace problem.
     
    , Oct 19, 2007
    #1
    1. Advertising

  2. In comp.lang.javascript message <
    legroups.com>, Fri, 19 Oct 2007 07:43:00, ""
    <> posted:
    >
    >The code examples are all plain green. I'll beautify them later (i do
    >this in my editor w/a regexp find/rep)
    >
    >http://dhtmlkitchen.com/learn/js/enumeration/



    I will not be reading it, because :

    You have overridden my chosen settings for font face and size.
    Your chosen font is sans-serif, and too small, and will not Zoom in IE6
    (OK, a heading Zooms; but having part-Zoom is silly).
    You force the width so that the lines are too long and have too many
    words.

    I did try, though, and had a thought re .toFixed examples, which also
    applies in the newsgroup FAQ and elsewhere : the example inputs to
    ..toFixed could be changed to be such that Javascript stores them
    exactly. It will then be more honestly obvious what the point at issue
    is. 0.07 can become 0.0625. Since 1.1255 cannot be stored exactly, but
    only as (I think)
    +1.1254999999999999449329379785922355949878692626953125
    it should round down.

    For ease of reading, a decimal point should always have at least one
    digit on each side, if possible.

    The pages seem so big that there probably should be a warning for dial-
    up users but I could be wrong there as Open Office is downloading
    currently.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Oct 20, 2007
    #2
    1. Advertising

  3. Guest

    On Oct 20, 10:16 am, Dr J R Stockton <> wrote:
    > In comp.lang.javascript message <
    > legroups.com>, Fri, 19 Oct 2007 07:43:00, ""
    > <> posted:
    >
    >
    >
    > >The code examples are all plain green. I'll beautify them later (i do
    > >this in my editor w/a regexp find/rep)

    >
    > >http://dhtmlkitchen.com/learn/js/enumeration/

    >
    > I will not be reading it, because :
    >
    > You have overridden my chosen settings for font face and size.
    > Your chosen font is sans-serif, and too small, and will not Zoom in IE6
    > (OK, a heading Zooms; but having part-Zoom is silly).
    > You force the width so that the lines are too long and have too many
    > words.
    >


    ctrl+/- works fine in IE7 and all other browsers. I don't have IE6, so
    I couldn't try it.

    Zoom is amazing in Opera!


    > I did try, though, and had a thought re .toFixed examples, which also
    > applies in the newsgroup FAQ and elsewhere : the example inputs to
    > .toFixed could be changed to be such that Javascript stores them
    > exactly. It will then be more honestly obvious what the point at issue
    > is. 0.07 can become 0.0625. Since 1.1255 cannot be stored exactly, but
    > only as (I think)
    > +1.1254999999999999449329379785922355949878692626953125
    > it should round down.
    >

    1.1254999999999999449329379785922355949878692626953125 === 1.1255; //
    true

    A more useful, pragmatic result of 1.1255.toFixed(3) would be 1.126,
    not 1.125. I believe that is the intent of the spec, but it falls
    short, unfortunately. I think Safari had the right approach. It might
    not be 100% compliant, but gets results that don't require mental
    binary conversions.



    There are 10 types of people in the world: Those who understand
    binary, and those who don't...


    > For ease of reading, a decimal point should always have at least one
    > digit on each side, if possible.
    >
    > The pages seem so big that there probably should be a warning for dial-
    > up users but I could be wrong there as Open Office is downloading
    > currently.
    >

    Nah, no warnings. The pages are only about 10-12k after gzip. The
    server response does seem pretty slow, though. I'm going to get a java
    profiler for that.

    I broke out a few examples into new pgs. I'll probably break out a few
    more pgs. It seems that the pages are too long. I should break them
    out and summarize.


    > --
    > (c) John Stockton, Surrey, UK. ? Turnpike v6.05 IE 6
    > news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    > <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    > <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    , Oct 20, 2007
    #3
  4. In comp.lang.javascript message <
    oglegroups.com>, Sat, 20 Oct 2007 20:36:23, ""
    <> posted:
    >On Oct 20, 10:16 am, Dr J R Stockton <> wrote:
    >> In comp.lang.javascript message <
    >> legroups.com>, Fri, 19 Oct 2007 07:43:00, ""
    >> <> posted:




    >> >http://dhtmlkitchen.com/learn/js/enumeration/

    >>
    >> I will not be reading it, because :
    >>
    >> You have overridden my chosen settings for font face and size.
    >> Your chosen font is sans-serif, and too small, and will not Zoom in IE6
    >> (OK, a heading Zooms; but having part-Zoom is silly).
    >> You force the width so that the lines are too long and have too many
    >> words.
    >>

    >
    >ctrl+/- works fine in IE7 and all other browsers. I don't have IE6, so
    >I couldn't try it.
    >
    >Zoom is amazing in Opera!


    Yes. But there are still a fixed number of words per line, so zooming
    in soon reaches the screen width. The whole approach is too dirigiste.

    >> I did try, though, and had a thought re .toFixed examples, which also
    >> applies in the newsgroup FAQ and elsewhere : the example inputs to
    >> .toFixed could be changed to be such that Javascript stores them
    >> exactly. It will then be more honestly obvious what the point at issue
    >> is. 0.07 can become 0.0625. Since 1.1255 cannot be stored exactly, but
    >> only as (I think)
    >> +1.1254999999999999449329379785922355949878692626953125
    >> it should round down.
    >>

    >1.1254999999999999449329379785922355949878692626953125 === 1.1255; //
    >true


    Yes, but it remains true for substantial changes in the LHS : in
    particular, 1.12550000000000005 === 1.1255 .

    >A more useful, pragmatic result of 1.1255.toFixed(3) would be 1.126,
    >not 1.125. I believe that is the intent of the spec, but it falls
    >short, unfortunately. I think Safari had the right approach. It might
    >not be 100% compliant, but gets results that don't require mental
    >binary conversions.


    Since 1.1255 is stored under-value, 1.1255.toFixed(3) should give 1.125.
    IE6 gives 1.126, Opera 9.24 and Firefox 2.0.0.8 give 1.125 .

    What should be said is that, if always getting the exact final digit
    matters, one should only use quantities, including intermediates, that
    can be represented exactly in IEEE Doubles. Input and output strings
    can be adjusted accordingly, as strings.


    >> --
    >> (c
    >> ne
    >> <U
    >> <U


    Please read the FAQ about news reply formatting.

    Consider Section 3(a) of the Uniform Time Act of 1966 (15 U.S.C.
    260a(a)) !


    You have :

    var day = 1000 * 60 * 60 * 24;
    var yesterday = new Date(new Date() -day );
    var today = new Date();
    var tomorrow = new Date(new Date() - (-day) );

    The second and fourth of those lines are wrong, unless the code is not
    to be used with certain date/time/place combinations. Without curing
    that fault, the fourth line is better written as

    var tomorrow = new Date(+new Date() + day);

    One can avoid the creation of a second Object by
    var D = new Date() ; D.setTime(+D + Day)
    or
    var D = new Date() ; D.setDate(D.getDate()+1)


    In Opera, Ctrl-Alt-V finds errors.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
     
    Dr J R Stockton, Oct 21, 2007
    #4
  5. On Oct 19, 12:43 am, "" <>
    wrote:
    > I have an article online, but it's not officially linked yet. This
    > will be the first public link.
    >
    > I've found tons of errors myself, and there's probably plenty more --
    > I just didn't find them yet.
    >
    > I'd really like to get some review on this. Both technical and
    > editorial, but mostly technical.
    >
    > The code examples are all plain green. I'll beautify them later (i do
    > this in my editor w/a regexp find/rep)
    >
    > http://dhtmlkitchen.com/learn/js/enumeration/


    The article is very long and it seems like the article could be broken
    into several smaller ones focused on one particular recommendation.
    For example, the article talks about broken dontEnum in JScript and
    also about the location of the JSON methods. Those seem like very
    separate issues.

    Peter
     
    Peter Michaux, Oct 22, 2007
    #5
  6. Guest

    On Oct 21, 7:24 pm, Peter Michaux <> wrote:
    > On Oct 19, 12:43 am, "" <>
    > wrote:


    >
    > >http://dhtmlkitchen.com/learn/js/enumeration/

    >
    > The article is very long

    Yes, 12 long pages + 2 popup examples! I really didn't want it to be
    more than 3-4 pages.

    and it seems like the article could be broken
    > into several smaller ones focused on one particular recommendation.
    > For example, the article talks about broken dontEnum in JScript and
    > also about the location of the JSON methods. Those seem like very
    > separate issues.
    >

    You're right.

    I'm trying to get Nicholas to write a contrasting proposal. That way,
    I could have a simple link;

    * Alterate JSON Proposal

    Instead of the lengthy (and irrelevant) JSON discussion.

    Time for a thread on that... :)


    > Peter
     
    , Oct 22, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Namratha Shah \(Nasha\)
    Replies:
    0
    Views:
    4,747
    Namratha Shah \(Nasha\)
    Nov 5, 2004
  2. =?Utf-8?B?TmF2ZWVu?=

    Article : Authentication and Authorization in ASP.Net

    =?Utf-8?B?TmF2ZWVu?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    360
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Oct 26, 2004
  3. Howard Jess
    Replies:
    16
    Views:
    177
  4. OOJS Question

    , Jun 28, 2007, in forum: Javascript
    Replies:
    1
    Views:
    72
    Manish Tomar
    Jun 28, 2007
  5. xdevel1999

    DontEnum with custom property

    xdevel1999, May 15, 2009, in forum: Javascript
    Replies:
    1
    Views:
    93
    Jeremy J Starcher
    May 15, 2009
Loading...

Share This Page