Web Analytics Made Easy -
StatCounter Do you like Javascript 2.0? - CodingForum

Announcement

Collapse
No announcement yet.

Do you like Javascript 2.0?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Do you like Javascript 2.0?

    I have recently have a deep look into the javascript 2.0 proposal. I have noticed that the proposal is for a new class oriented language. This is weird for me, as I used to consider Javascript a prototyped-oriented language, and I can not say I am delighted about the new class look of the Javascript. From my side of the street, that will violate the "principle of least surprise" (programmers know what I mean).

    What is your opinion. Does javascript worth to change into a class language, or it should remain a prototype?

    My vote is for a prototype. I like the flexibility. And within some conditions, a prototyped language can be almost as fast as a class one, whithin fewer code lines.
    10
    Should become class based
    20.00%
    2
    Should remain prototype based
    80.00%
    8
    Last edited by Kor; Oct 8, 2006, 08:52 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    Well, this is "Netscape's" JavaScript 2.0 proposal from 2003. A bunch of the proposal is outdated. I don't know the current state of the proposal and I wonder if there aren't other proposals.

    I think that a package system would be incredibly helpful, and I would welcome it. I think the proposal makes good points about it too. If they make it easy to package code, then there will be lots of problems with obtrusion. They seem to think that a class based system would solve that because then package writers can protect members and methods.

    I think that a prototyped system does a lot of things well. I also think that taking some features from a classed system would be inredibly useful. I think that writing a package would be made easier if you could prevent certain elements from being overwritten, but I don't think it's completely necessary. We can create public and private members and methods already, so anything that's very important to keep protected can be made private.

    However, I think if we added a new type of object, specifically a class, with new syntax for definition, that allows for PROTECTED members and methods, and makes inheritance easier, we would definitely benefit from it. As long as we keep the same core pieces like creating new Objects from other objects, keeping the prototypes, keeping the expando properties, we should be able to continue what we're doing and get more power.

    I don't like how they want to have two ways of executing, one for legacy, and one for the new class-based version. I do like the idea of const and final and what not.

    I think the biggest problems with programming JavaScript stem from the environment it runs in. As a language, it can definitely be improved, but I don't think it should become completely class based, because the prototype based paradigm works, works well, and can be modified to improve it, no need to scrap it.

    Comment


    • #3
      Yeah, seems kind of silly to change javascript into a class based system. With the little web design I have done, this seems excessive
      "To iterate is human, to recurse divine." -L. Peter Deutsch

      Comment


      • #4
        I think they should have changed the prototype system to fix the problems in it as it is instead of adding another object hierarch and inheritance system, but anyway the improved language will be better than the old.
        liorean <[[email protected]]>
        Articles: RegEx evolt wsabstract , Named Arguments
        Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
        Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

        Comment


        • #5
          The necessary words were reserved from day one to allow the language to be converted to class based. JScript.net is not identical to the Javascript 2.0 specification (what Microsoft written software ever is) but it is equivalent and is effectively a class based version of javascript that runs on the server.
          Stephen
          Learn Modern JavaScript - http://javascriptexample.net/
          Helping others to solve their computer problem at http://www.felgall.com/

          Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

          Comment


          • #6
            I agree with Beagle: add features, but don't change the language.

            I don't think that javascript 2.0 should have to be changed into a class based language... Javascript is extensible and allready has an Object oriented prototype system that supports inheritance.

            I learned Java before JavaScript, so I would have liked the class based system if it had been in place from the beginning, but now we're stuck with the prototypes.

            You could easily add "classes" to javascript consider the instanceof operator already in javascript...

            Inheritance is simple in O.O. Javascript right now.
            Code:
            function ParentClass(x,y){
              this.x = x;
              this.y = y;
              function privateFunction(){
                alert("private");
              }
            }
            function ChildClass(x,y,z){
              this.parentRef = ParentClass; //create a refrence to the parent class
              this.parentRef(x,y); //Call the constructor of parent to inherit its properties.
              this.z = z;
              this.publicFunction = function(){
                alert("public");
              }
              this.toString=function(){
                return [this.x,this.y,this.z].join(', ');
              }
            }
            var myObject = new ChildClass(0,1,2);
            alert("myObject is an instance of ChildClass:"+
              (myObject instanceof ChildClass)+
              "\n"+myObject.toString()
            );
            What's wrong with what we've already got?

            Comment


            • #7
              Inheritance is problematic because you can't have protected members, which is actually a very useful thing when it comes to inheritance. I've had to make special consideration to copy private members from super classes to sub classes in order to give the sub class access to the member.

              Also, currently, there is no way to reference private variables directly, you have go through your scope chain. In most OO languages, they provide a reference to the object directly. JavaScript does have the "this" keyword, but you can't reference private members or methods using "this". Because of that, complex scope resolution becomes multi-stepped and therefore takes more time than necessary.

              Comment


              • #8
                Originally posted by Beagle View Post
                Inheritance is problematic because you can't have protected members, which is actually a very useful thing when it comes to inheritance. I've had to make special consideration to copy private members from super classes to sub classes in order to give the sub class access to the member.

                Also, currently, there is no way to reference private variables directly, you have go through your scope chain. In most OO languages, they provide a reference to the object directly. JavaScript does have the "this" keyword, but you can't reference private members or methods using "this". Because of that, complex scope resolution becomes multi-stepped and therefore takes more time than necessary.
                If you used it and if the code turns into a self-recursive closure.
                KOR
                Offshore programming
                -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                Comment


                • #9
                  Originally posted by Beagle View Post
                  Inheritance is problematic because you can't have protected members, which is actually a very useful thing when it comes to inheritance. I've had to make special consideration to copy private members from super classes to sub classes in order to give the sub class access to the member.
                  So... in JS private methods/properties can actually be accessed from outside the object they're defined in? Weird.
                  I'm not sure if this was any help, but I hope it didn't make you stupider.

                  Experience is something you get just after you really need it.
                  PHP Installation Guide Feedback welcome.

                  Comment


                  • #10
                    Originally posted by marek_mar View Post
                    So... in JS private methods/properties can actually be accessed from outside the object they're defined in? Weird.
                    There is no such thing as private as far as Javascript<2 is concerned. You emulate private members via scoping constructs, while "public" members are just assigned directly as properties of the object.

                    I think JS2.0 is a welcome addition. Everything you can do with Javascript as it is still remains possible. I think prototypes are way more useful for web scripting than classes. But the addition of classes just makes JS development more accessible to those proficient in other class-based OOP languages. What I'm more excited about is the optional type checking and subsequently method and operator overloading. With overloading you can implement true type transparency, which seems very much in line with the philosophy of Javascript.
                    jasonkarldavis.com

                    Comment


                    • #11
                      Originally posted by Kor View Post
                      If you used it and if the code turns into a self-recursive closure.
                      I don't think the speed problem only exists if you have self-recursion. If you have multiple layers of abstraction, every step away from the definition of the variable you are it another resolution. In complex OO structures using JavaScript, simply accessing a member can be time consuming because there's no easy way to specify the scope the variable is defined in, you simply have to let the engine run through each scope in the chain.

                      I don't know if the JavaScript 2.0 proposal would fix that, I think they are intending to though.

                      jkd's comments are spot on, if in fact the JavaScript 2.0 proposal would leave everything the way it currently is and add classes to it. I didn't exactly get that feeling from them though. It seemed like the proposal wanted to have both static and dynamic classes, but I didn't read enough about keeping javascript the way it is while simply adding new features. I do agree that type "hinting" and function overloading would be an incredible boon to development and predictability of code.

                      marek_mar: I meant "protected" in the technical sense, not the literal sense. No, you cannot access "private" members in javascript through external means. Protected members are those which are accesible to sub-classes of the parent class. In JavaScript, as Kor said, we simulate private encapsulation through scoping variables as temporary to the scope, which means they are only accesible to the scope that defined them. If we attempt to implement object inheritance, the new object's scope cannot access the parent's "private variables". In class based languages, the concept of protected variables is very useful for providing strong encapsulation while keeping the extensibility.

                      Comment


                      • #12
                        I'll be going back to my PHP5 OOP without even a trace of regret.
                        Classes in JS will get my vote.
                        I'm not sure if this was any help, but I hope it didn't make you stupider.

                        Experience is something you get just after you really need it.
                        PHP Installation Guide Feedback welcome.

                        Comment

                        Working...
                        X