Strongly Typed Language to JavaScript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Praveen Ramesh

    Strongly Typed Language to JavaScript

    Hi,

    I just started writing a lot of script code in JavaScript (accessing
    browser DOM basically) and coming from the C#-VS.Net background, I
    just hate it. There is no true class definition support and no
    code-editor support for code-completion, not to mention debugging and
    stuff.

    Do you guys know of any good code-editor that might make my life
    easier - with code completion support, etc.?

    What would be much better is if I can write code in C# (with my class
    definitions and stuff, and aginst a library that mimics the DOM types)
    in VS.Net (with all the awesome editor support), compile it - to catch
    all errors and then convert it to JavaScript! Is there some such C# to
    JavaScript tool available? If not, wouldn't that be a good idea?

    -Praveen
  • Robert

    #2
    Re: Strongly Typed Language to JavaScript

    rpraveen@yahoo. com (Praveen Ramesh) wrote in message news:<4fd2ca58. 0410200812.7c72 2dde@posting.go ogle.com>...[color=blue]
    > Hi,
    >
    > I just started writing a lot of script code in JavaScript (accessing
    > browser DOM basically) and coming from the C#-VS.Net background, I
    > just hate it. There is no true class definition support[/color]

    Perhaps data encapsolation via closures would be helpful. Notice how
    the variable accumulate is used.

    Robert

    <html>
    <head>
    <title>encapsol ated data</title>
    <script type="text/javascript" >
    function util()
    {
    // This is the encapsolated variable.

    var accumulate = 0;

    function add()
    {
    accumulate += 5;
    show();
    }

    function show()
    {
    alert("accumula te = " + accumulate);
    }

    // Make know the inner functions.

    util.add = add;
    util.display = show;
    }
    </script>
    </head>
    <body>
    <script type="text/javascript" >
    // Initial
    util();


    util.add();
    util.add();

    util.display();
    </script>
    <p>We have Demonstrated encapsolated data through the use
    of a closure.</p>
    </body>
    </html>

    Comment

    • lawrence

      #3
      Re: Strongly Typed Language to JavaScript

      rccharles@my-deja.com (Robert) wrote in message news:<c6bb75ff. 0410221154.6afb 1309@posting.go ogle.com>...[color=blue]
      > rpraveen@yahoo. com (Praveen Ramesh) wrote in message
      > <script type="text/javascript" >
      > function util()
      > {
      > // This is the encapsolated variable.
      >
      > var accumulate = 0;
      >
      > function add()
      > {
      > accumulate += 5;
      > show();
      > }
      >
      > function show()
      > {
      > alert("accumula te = " + accumulate);
      > }
      >
      > // Make know the inner functions.
      >
      > util.add = add;
      > util.display = show;
      > }
      > </script>
      > </head>
      > <body>
      > <script type="text/javascript" >
      > // Initial
      > util();
      >
      >
      > util.add();
      > util.add();
      >
      > util.display();
      > </script>[/color]


      I'm a newbie and still trying to get the syntax. What does this mean?
      [color=blue]
      > util.add = add;
      > util.display = show;[/color]

      If add() is a method then shouldn't it be followed by parenthesises?

      Comment

      • Michael Winter

        #4
        Re: Strongly Typed Language to JavaScript

        On 6 Nov 2004 09:24:36 -0800, lawrence <lkrubner@geoci ties.com> wrote:
        [color=blue]
        > rccharles@my-deja.com (Robert) wrote in message
        > news:<c6bb75ff. 0410221154.6afb 1309@posting.go ogle.com>...
        >[color=green]
        >> <script type="text/javascript" >
        >>
        >> function util()
        >> {
        >> // This is the encapsolated variable.
        >>
        >> var accumulate = 0;
        >>
        >> function add()
        >> {
        >> accumulate += 5;
        >> show();
        >> }
        >>
        >> function show()
        >> {
        >> alert("accumula te = " + accumulate);
        >> }
        >>
        >> // Make know the inner functions.
        >>
        >> util.add = add;
        >> util.display = show;
        >> }
        >> </script>
        >> </head>
        >> <body>
        >> <script type="text/javascript" >
        >> // Initial
        >> util();
        >>
        >>
        >> util.add();
        >> util.add();
        >>
        >> util.display();
        >> </script>[/color]
        >
        > I'm a newbie and still trying to get the syntax. What does this mean?[/color]

        The first thing to understand is that a function is an object, just a
        special type that can be "called". Like other objects, it can have
        properties and in the code above, properties are added to this object.

        The second is related to the first. As you may, or may not, be aware,
        objects are accessed via references. This means that two or more
        identifiers can refer to the same object, and any change made through one
        reference is reflected through all other identifiers that refer to the
        same object. Perhaps some code in order here:

        // Both myObj and myRef will refer to the same object
        var myObj = new Object(),
        myRef = myObj;

        myObj.member = 'a string'; // assign through first reference

        alert(myObj.mem ber); // 'a string';
        alert(myRef.mem ber); // also 'a string';

        myRef.member = 15; // assign through the second reference

        alert(myObj.mem ber); // 15;
        alert(myRef.mem ber); // also 15;

        So, extending this to functions:

        function myFunction() {
        alert('called myFunction');
        }

        var myRef = myFunction;

        myRef(); // alerts 'called myFunction'

        Hopefully that's laid some groundwork.

        In the function, util, two inner functions, add and display, are defined.
        Inner functions act just like local variables: they are not visible
        outside the scope of the outer function that declares them.
        [color=blue][color=green]
        >> util.add = add;
        >> util.display = show;[/color]
        >
        > If add() is a method then shouldn't it be followed by parenthesises?[/color]

        What the code does in this instance is make the inner functions properties
        of the outer function. Like I've shown previously, those two lines create
        two references to the inner functions. Because you aren't actually calling
        the functions - there are no parentheses - you are assigning the
        *function* to the property, not the result of the function.

        However, I don't think I like that particular production much as calling
        util again will reset the data. If that's intended, then fine. If not,

        var util = (function() {
        var accumulate = 0;

        return {
        add : function(n) {accumulate += n;},
        show : function() {alert(accumula te);}
        };
        })();

        would be better as the setup is a one-time-only process.

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • lawrence

          #5
          Re: Strongly Typed Language to JavaScript

          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opsg2di2t dx13kvk@atlanti s>...[color=blue]
          >The first thing to understand is that a function is an object, just a
          >special type that can be "called". Like other objects, it can have
          >properties and in the code above, properties are added to this[/color]
          object.

          Are there any other computer languages where you can add a property to
          a class after the class has been defined? I know in Ruby you can add
          even methods to a class at run-time, so I suppose Ruby allows you to
          add class properties as well. But are there any others? I'm used to
          the idea of a class being defined, and then remaining fixed for the
          rest of its life.

          Comment

          • lawrence

            #6
            Re: Strongly Typed Language to JavaScript

            "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opsg2di2t dx13kvk@atlanti s>...[color=blue]
            > function myFunction() {
            > alert('called myFunction');
            > }
            >
            > var myRef = myFunction;
            >
            > myRef(); // alerts 'called myFunction'[/color]

            I see. So myFunction(), with parenthesises causes the function to
            fire, but myFunction, without parenthesises, passes a reference to
            that function and allows one, thereafter, to treat it as an object?

            Comment

            • Grant Wagner

              #7
              Re: Strongly Typed Language to JavaScript

              lawrence wrote:
              [color=blue]
              > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message news:<opsg2di2t dx13kvk@atlanti s>...[color=green]
              > > function myFunction() {
              > > alert('called myFunction');
              > > }
              > >
              > > var myRef = myFunction;
              > >
              > > myRef(); // alerts 'called myFunction'[/color]
              >
              > I see. So myFunction(), with parenthesises causes the function to
              > fire, but myFunction, without parenthesises, passes a reference to
              > that function and allows one, thereafter, to treat it as an object?[/color]

              myFunction not "treated as an object", it is a reference to a Function object. The same as any other
              reference to any other object.

              var a = new Object();
              var b = a; // both -b- and -a- point to the same object
              b.one = 1;
              alert(a.one); // 1

              var a = function() {
              alert('function -a-');
              }
              // same as:
              // function a() {
              // alert('function -a-');
              // }
              b = a; // both -b- and -a- point to the same object (function)
              b();

              So:
              window.onload = function() { alert('loaded') ; }
              or:
              function a() { alert('loaded') ; }
              window.onload = a;

              constructs a new (anonymous in the first case) Function object and assigns the reference to that Function
              object to the -onload- property of the -window-. When the document finishes loading, the host environment
              invokes the Function object referenced by the -onload- property of the -window- object.

              --
              Grant Wagner <gwagner@agrico reunited.com>
              comp.lang.javas cript FAQ - http://jibbering.com/faq

              Comment

              • Michael Winter

                #8
                Re: Strongly Typed Language to JavaScript

                On 16 Nov 2004 09:17:56 -0800, lawrence <lkrubner@geoci ties.com> wrote:

                [snip]
                [color=blue]
                > Are there any other computer languages where you can add a property to a
                > class after the class has been defined?[/color]

                Javascript isn't class-based. I could give a comparison, but that would
                take a long time, particularly if I went into inheritance techniques.
                Instead, take a look at Douglas Crockford's web site
                (<URL:http://www.crockford.c om/>). The "survey" covers the main elements
                of the language, and other articles examine more advanced areas in detail.
                [color=blue]
                > I know in Ruby you can add even methods to a class at run-time, so I
                > suppose Ruby allows you to add class properties as well. But are there
                > any others?[/color]

                I couldn't comment on Ruby as I haven't used it. However, I am familiar
                with several programming languages, and ECMAScript is the only truly
                dynamic one among them.

                [snip]

                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                • Michael Winter

                  #9
                  Re: Strongly Typed Language to JavaScript

                  On 16 Nov 2004 09:30:41 -0800, lawrence <lkrubner@geoci ties.com> wrote:
                  [color=blue]
                  > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                  > news:<opsg2di2t dx13kvk@atlanti s>...
                  >[color=green]
                  >> function myFunction() {
                  >> alert('called myFunction');
                  >> }
                  >>
                  >> var myRef = myFunction;
                  >>
                  >> myRef(); // alerts 'called myFunction'[/color]
                  >
                  > I see. So myFunction(), with parenthesises causes the function to fire,[/color]

                  Correct.
                  [color=blue]
                  > but myFunction, without parenthesises, passes a reference to that
                  > function and allows one, thereafter, to treat it as an object?[/color]

                  I'm not sure that you've understood. There is no "passing of a reference",
                  and a function *is* an object, just a special kind that can be "called".
                  Perhaps I'm just attaching meaning to the words that you didn't intend
                  (that's why using the right vocabulary is important).

                  Functions can have properties just like any other object. Functions can
                  even have methods. The only thing that makes them different to "regular"
                  objects is that when you put () after the reference, you can have it
                  execute code.

                  Mike

                  --
                  Michael Winter
                  Replace ".invalid" with ".uk" to reply by e-mail.

                  Comment

                  • Richard Cornford

                    #10
                    Re: Strongly Typed Language to JavaScript

                    lawrence wrote:[color=blue]
                    > Michael Winter wrote:[color=green]
                    >> The first thing to understand is that a function is an object,
                    >> just a special type that can be "called". Like other objects,
                    >> it can have properties and in the code above, properties are
                    >> added to this object.[/color]
                    >
                    > Are there any other computer languages where you can add a
                    > property to a class after the class has been defined?[/color]

                    Javascript doesn't have classes. By ECMA specification all objects
                    (except host objects) are of the same; The ECMAScript native object.
                    That object allows the dynamic creation of new properties at run time.
                    Objects that have been modified in the same way (either internally by
                    the interpreter or by script code) can be conceived as belonging to a
                    distinct 'class' , but that would be done for the convenience of the
                    author and would not represent a real imposition on the objects.

                    <snip>[color=blue]
                    > I'm used to the idea of a class being defined, and
                    > then remaining fixed for the rest of its life.[/color]

                    When you have more experience of cross-browser scripting you will
                    appreciate the advantages of having a truly dynamic language in which to
                    write them.

                    Richard.


                    Comment

                    Working...