OO JavaScript? Who's using it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Fitzpatrick

    OO JavaScript? Who's using it?

    Just by way of introduction, I'm currently the principal developer and
    maintainer of the a JavaScript editor plug-in for Eclipse.

    [https://sourceforge.net/projects/jseditor/ The plug-in as it stands
    supports syntax highlighting and outlining of functions, classes and
    their methods.]

    When I took over the project in June it was for the purpose of adding
    outlining facilities for JavaScript written in an OO style with classes
    and so on. I have since discovered, to both my delight and horror, that
    there are a variety of ways of accomplishing OO in JavaScript.

    I was wondering if any of those people currently involved in large
    JavaScript projects might be willing to provide me with some input on:

    - the style they use
    - what kind of editor support they would find useful

    I look forward to your input. (And if you don't feel the newsgroup is
    the right place for this, there are discussion forums on the website
    maintainted by sourceforge...)

    Cheers! [And season's greetings!]

    --
    Alex
  • Richard Cornford

    #2
    Re: OO JavaScript? Who's using it?

    "Alex Fitzpatrick" <alex.nospam.fi tzpatrick@video tron.nospam.ca> wrote
    in message news:6E6Gb.1848 3$%o4.239510@wa gner.videotron. net...
    <snip>[color=blue]
    >When I took over the project in June it was for the purpose
    >of adding outlining facilities for JavaScript written in an
    >OO style with classes and so on.[/color]

    It has often been questioned in this group whether JavaScript has any
    structures that can reasonably be labelled a "class", or whether any
    terminology from class-based languages should be applied to JavaScript.

    Personally I don't mind using "class" to describe a particular sub-set
    of JavaScript object definitions, but only as a convenient label and
    only if the corresponding code is employed in a way that does not render
    the term inapplicable (Given the runtime mutability of both object
    instances and constructor prototypes).
    [color=blue]
    >I have since discovered, to both my delight and horror,
    >that there are a variety of ways of accomplishing OO in
    >JavaScript.[/color]

    And there is the problem, JavaScript is wonderfully flexible both in
    terms of the structures that can be defined and the way in which they
    can be employed.

    There is also the question of what separates an object from a function
    (as functions are function objects) and so: when would a code structure
    that resulted in instances of function objects with public and possibly
    (closure emulated) private members (and possibly also public static and
    (closure emulated) private static members) qualify as equivalent to a
    "class"? A question that might be best illustrated with code:-

    var TimedQue = (function(){
    var base, timer;
    var interval = 50;
    var newFncs = null;
    function addFnc(next, f){
    function t(){
    next = next&&next();
    if(f()){
    return t;
    }else{
    f = null;
    return next;
    }
    };
    t.addItem = function(d){
    if(next){
    next.addItem(d) ;
    }else{
    next = d;
    }
    return this;
    };
    t.finalize = function(){
    return ((next)&&(next = next.finalize() )||(f = null));
    };
    return t;
    };
    function tmQue(fc){
    if(newFncs){
    newFncs = newFncs.addItem (addFnc(null, fc));
    }else{
    newFncs = addFnc(null, fc);
    }
    if(!timer){
    timer = setTimeout(tmQu e.act, interval);
    }
    };
    tmQue.act = function(){
    var fn = newFncs, strt = new Date().getTime( );
    if(fn){
    newFncs = null;
    if(base){
    base.addItem(fn );
    }else{
    base = fn;
    }
    }
    base = base&&base();
    if(base||newFnc s){
    var t = interval - (new Date().getTime( ) - strt);
    timer = setTimeout(tmQu e.act, ((t > 0)?t:1));
    }else{
    timer = null;
    };
    };
    tmQue.act.toStr ing = function(){
    return 'TimedQue.act() ';
    };
    tmQue.finalize = function(){
    timer = timer&&clearTim eout(timer);
    base = base&&base.fina lize();
    newFncs = [];
    };
    return tmQue;
    })();

    TimedQue is a DHTML animation timer. The object assigned to the named
    global variable is a function and it is employed by calling it with a
    function reference as an argument. It adds the function to an internal
    queue and then arranges that the function be repeatedly executed at
    intervals so long as it returns true, dropping the function from the
    queue as soon as its execution returns false (or type-converted
    equivalent).

    From one point of view TimedQue is just a function but it has two public
    methods and might be said to have 5 private members. But a better
    candidate for consideration is the function - t - returned by calls to -
    addFnc - as it two has public methods and multiple instances of the
    function object, defined with the code within the - addFnc - function,
    are created. So does the code within the - addFnc - function qualify as
    an inner "class" definition?

    From the point of view of wanting to apply class-based terminology to
    JavaScript TimedQue is an (the) instance of a singleton class with 2
    public methods and 5 private members including a private factory method
    that returns instances of the - t - inner class. It is going to be a
    pain to try to pull that interpretation out from the source code, and
    there is not one single instance of the JavaScript - Object - "class"
    created or employed anywhere in the above code.
    [color=blue]
    >I was wondering if any of those people currently involved
    >in large JavaScript projects might be willing to provide
    >me with some input on:[/color]

    Given the date, is anyone currently involved in anything? ;)
    [color=blue]
    > - the style they use[/color]

    That is not an easy question to answer. The flexibility that JavaScript
    offers means that tying yourself down to a ridged concept of "class" or
    any one pattern of object definition/instantiation might be working
    against the strengths of the language. I tend to be flexible and
    consider what the code is going to do as the various styles all involve
    some trade-off or another.
    [color=blue]
    > - what kind of editor support they would find useful[/color]

    I am not sure, I tend to use textPad though it only recognises 6 keyword
    classes when syntax highlighting and I would like probably 10 or so.

    Richard.


    Comment

    • Alex Fitzpatrick

      #3
      Re: OO JavaScript? Who's using it?

      Richard Cornford wrote:[color=blue]
      > "Alex Fitzpatrick" <alex.nospam.fi tzpatrick@video tron.nospam.ca> wrote
      > in message news:6E6Gb.1848 3$%o4.239510@wa gner.videotron. net...
      > <snip>
      >[color=green]
      >>When I took over the project in June it was for the purpose
      >>of adding outlining facilities for JavaScript written in an
      >>OO style with classes and so on.[/color]
      >
      >
      > It has often been questioned in this group whether JavaScript has any
      > structures that can reasonably be labelled a "class", or whether any
      > terminology from class-based languages should be applied to JavaScript.
      >
      > Personally I don't mind using "class" to describe a particular sub-set
      > of JavaScript object definitions, but only as a convenient label and
      > only if the corresponding code is employed in a way that does not render
      > the term inapplicable (Given the runtime mutability of both object
      > instances and constructor prototypes).[/color]

      A very good point, but this would be very a confusing distinction for
      many people, so I don't try to go there outside of an academic setting.

      Perhaps what we need is a strong etymology of class-like constructs that
      are possible in a prototype oriented language. Clearly class-like is
      just one.
      [color=blue][color=green]
      >>I have since discovered, to both my delight and horror,
      >>that there are a variety of ways of accomplishing OO in
      >>JavaScript.[/color]
      >
      >
      > And there is the problem, JavaScript is wonderfully flexible both in
      > terms of the structures that can be defined and the way in which they
      > can be employed.
      >
      > There is also the question of what separates an object from a function
      > (as functions are function objects) and so: when would a code structure
      > that resulted in instances of function objects with public and possibly
      > (closure emulated) private members (and possibly also public static and
      > (closure emulated) private static members) qualify as equivalent to a
      > "class"? A question that might be best illustrated with code:-
      >[/color]

      -Example snipped for brevity- - nice example though-

      This kind of syntax should be supportable, right down to the "inner"
      class. JSEditor does not currently handle private members or inner
      classes, but I'm hoping to add this soon.
      [color=blue]
      > From one point of view TimedQue is just a function but it has two public
      > methods and might be said to have 5 private members. But a better
      > candidate for consideration is the function - t - returned by calls to -
      > addFnc - as it two has public methods and multiple instances of the
      > function object, defined with the code within the - addFnc - function,
      > are created. So does the code within the - addFnc - function qualify as
      > an inner "class" definition?[/color]

      Yup, and TimedQue is just implementing a Factory pattern! :-)
      [color=blue]
      > From the point of view of wanting to apply class-based terminology to
      > JavaScript TimedQue is an (the) instance of a singleton class with 2
      > public methods and 5 private members including a private factory method
      > that returns instances of the - t - inner class. It is going to be a
      > pain to try to pull that interpretation out from the source code, and
      > there is not one single instance of the JavaScript - Object - "class"
      > created or employed anywhere in the above code.[/color]

      No problem at all. I just wish I could prove that today. Perhaps you've
      just given me something to target for January.
      [color=blue][color=green]
      >>I was wondering if any of those people currently involved
      >>in large JavaScript projects might be willing to provide
      >>me with some input on:[/color]
      >
      > Given the date, is anyone currently involved in anything? ;)
      >[/color]

      Everyone's a comedian on UseNet. :-)
      [color=blue][color=green]
      >> - the style they use[/color]
      >
      >
      > That is not an easy question to answer. The flexibility that JavaScript
      > offers means that tying yourself down to a ridged concept of "class" or
      > any one pattern of object definition/instantiation might be working
      > against the strengths of the language. I tend to be flexible and
      > consider what the code is going to do as the various styles all involve
      > some trade-off or another.[/color]

      How about some more examples, that previous one was good.
      [color=blue][color=green]
      >> - what kind of editor support they would find useful[/color]
      >
      > I am not sure, I tend to use textPad though it only recognises 6 keyword
      > classes when syntax highlighting and I would like probably 10 or so.[/color]

      What keyword classes would those be?

      How about class browsing like Smalltalk or the Java Browsing Perspective
      in Eclipse?

      How many class-like behaviors do you maintain?

      [I got started in this project because my employer was looking at a
      project that would lead to hundreds of JavaScript classes. Doing this
      with several developers meant that TextPad was not sufficient and
      Eclipse offered no license fees and Perforce integration. (And some
      future hope of UML integration.)]

      Thanks for your input, it's nice to hear from others in the community.
      [It was also nice to see some elegant code that I understood even though
      I would have expressed it differently.]

      --
      Alex

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: OO JavaScript? Who's using it?

        Alex Fitzpatrick wrote:
        [color=blue]
        > Richard Cornford wrote:[color=green]
        >> Personally I don't mind using "class" to describe a particular sub-set
        >> of JavaScript object definitions, but only as a convenient label and
        >> only if the corresponding code is employed in a way that does not render
        >> the term inapplicable (Given the runtime mutability of both object
        >> instances and constructor prototypes).[/color]
        >
        > A very good point, but this would be very a confusing distinction for
        > many people,[/color]

        So these people should learn the language that they are using in *any* way.
        [color=blue]
        > so I don't try to go there outside of an academic setting.
        >
        > Perhaps what we need is a strong etymology of class-like constructs that
        > are possible in a prototype oriented language. Clearly class-like is
        > just one.[/color]

        Class-like is simply worng. Prototypes are fundamentally different
        from classes since every object can be the prototype of another, and
        OTOH two objects can have different properties even if they have
        been created with the same constructor function. In prototype-based
        languages like JavaScript 1.x, there is no strict class-binding that
        imposes something else.


        PointedEars, currently using JSEditor 0.3.3

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: OO JavaScript? Who's using it?

          Alex Fitzpatrick wrote:[color=blue]
          > Just by way of introduction, I'm currently the principal developer and
          > maintainer of the a JavaScript editor plug-in for Eclipse.[/color]

          Thank you, and keep up the good work.
          [color=blue]
          > [https://sourceforge.net/projects/jseditor/ The plug-in as it stands
          > supports syntax highlighting and outlining of functions, classes and
          > their methods.]
          >
          > When I took over the project in June it was for the purpose of adding
          > outlining facilities for JavaScript written in an OO style with classes
          > and so on. I have since discovered, to both my delight and horror, that
          > there are a variety of ways of accomplishing OO in JavaScript.[/color]

          And there are no classes in JavaScript 1.x.
          [color=blue]
          > I was wondering if any of those people currently involved in large
          > JavaScript projects might be willing to provide me with some input on:
          >
          > - the style they use[/color]

          You need to be more specific. For now, read my postings here and in
          de.comp.lang.ja vascript (if you can manage it), have a look at
          http://pointedears.de.vu/scripts/ and feed the scripts to the plugin.
          [color=blue]
          > - what kind of editor support they would find useful[/color]

          More and more reliable syntax highlighting, see my library
          documentations (.htm) for examples (they use the style.css
          file). With eclipse 3.0M4 and JSEditor 0.3.3, which I
          understand for the latter is the latest release, the plugin
          tends to forget where syntax highlighting should be applied.
          Example: I often type

          var foobar

          where `var' is sometimes not hightlighted. When I then remove
          the `v' and include it again, the keyword is finally highlighted.

          Only constructor functions where the `this' special operator is
          used are considered constructor functions by the plugin (then
          falsely displayed as (C)lass in the Outline view). This is
          obviously caused by the misguided attempt to impose class-based
          terminology over a prototype-based language (see my other posting).
          A function should be at least considered a constructor function if
          it contains the `this' operator or is called with the `new' operator.
          Example: math.js, where Exception is considered a "class" while
          specialized exception functions are not, even if they are called
          with the `new' operator in common functions.

          When properties are unconditionally added to the prototype property
          of an object, they should also appear as properties in the Outline
          view.

          A built-in JavaScript beautifier (and uglyfier ;-)) is needed.
          Unlike the Java Editor's formatter, which was IIRC usable for
          formatting JavaScript until eclipse 3.0M3, it needs to pay
          attention to dynamic HTML, so that `<' within a string is not
          considered an operator aso.

          It would be nice if one could create a coder's documentation from
          JavaScript source using special comments, like JavaDoc and PHPdoc
          already do. Thus I have recently developed JavaScriptDoc[tm]:



          A working implementation of it in JavaScript (using quite cruel RegExp
          parsing) is underway, but as the eclipse platform may already provide a
          parser suitable for JavaScriptDoc, I would be glad if it could be part
          of the JSEditor plugin once possible syntax inconsistencies have been
          resolved.

          These are my suggestions so far.

          (Unlike your From header, headers of my postings conform to Internet
          standards, and) I also welcome replies via electronic mail.


          PointedEars

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: OO JavaScript? Who's using it?

            Thomas 'Ingrid' Lahn wrote:
            [color=blue]
            > PointedEars, currently using JSEditor 0.3.3[/color]

            D'oh! It's 0.0.3, of course.

            And I see that 0.0.4 is now available. D'oh again? ;-)


            PointedEars, upgrading

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: OO JavaScript? Who's using it?

              Thomas 'Ingrid' Lahn wrote another monologue ;-)
              [color=blue]
              > Thomas 'Ingrid' Lahn wrote:[color=green]
              >> PointedEars, currently using JSEditor 0.3.3[/color]
              >
              > D'oh! It's 0.0.3, of course.
              >
              > And I see that 0.0.4 is now available. D'oh again? ;-)[/color]

              JFYI: JSEditor 0.0.4 does not work with eclipse 3.0M4 (the preferences
              page is missing and the editor cannot be associated.) So I stick to
              0.0.3 until I have the opportunity to download the latest platform
              milestone (> 70M) via DSL.


              PointedEars

              Comment

              Working...