Slashdot Mirror


Should JavaScript Get More Respect?

An anonymous reader points out an article in IBM's Crossing Borders series about the language features of JavaScript, surely the Rodney Dangerfield of scripting languages. But with increasing use in such technologies as Ajax, Apache Cocoon, ActionScript, and Rhino, some industry leaders are taking a fresh look at the language. From the article: "Nearly every Web developer has cursed JavaScript at one time or another. Until recently, many developers had all but written off JavaScript as a necessary evil at best or a toy at worst... But JavaScript is becoming increasingly important, and it remains the most broadly available scripting language for Web development."

8 of 439 comments (clear)

  1. Re:Why the pressure ? by tpwch · · Score: 4, Informative

    Java and Javascript? Why would anyone do that, since those two are not related other than the name. Sun developed Java, and Netscape developed Javascript. Totally independent of each other. I'm starting to get tired of people thinking that they have something to do with each other.

    --
    Posted by a Debian GNU/Linux user
  2. Re:Yes, but Javascript is a bad language. by Myen · · Score: 4, Informative
    Every function has a variable number of arguments. You can access it via the keyword arguments - it's an array. So yes.

    function foo() {
      alert(arguments[0])
    }
  3. I love the autopointerage & hate the scope iss by cyclomedia · · Score: 5, Informative
    Would be nice to have everything my way, the sheer built in extensibility of the language is in my opinion nothing short of beautiful, and something other languages could do well to imitate (check out D, for example).

    allow me to elaborate, suppose you want to know if the version of the language on your platform supports an intrinsic array push function, and if not, attatch your own:

    if( !Array.push )
      Array.prototype.push = function( item ){ ... }
    firstly the reference to .push in the if has no brackets, so it becomes a pointer to the function within the intrinsic Array class. you can then create a function and assign it to that pointer. Sheer magic and gorgeously intuitive.

    sticking with arrays you can grow and shrink them with little to zero fuss:

    function array_push( arr , item )
      arr[arr.length] = item;
    magically the array is one index longer. you can just set arr.length and it will append or delete indexes for you.

    you can also use this to assign functions to other object's handlers, most notibly events

    someObject.onclick = myFunction
    But this has brought up the thing that really really needs fixing, suppose i want that onclick function to pass some info to myFunction when i call it i have to do this

    someObject.onclick = function(){ myFunction( this.someAttribute ) }
    so instead i've created a function inline to hold my custom function, firstly it's not immediatley obvious to what object the "this" applies. if i'm running this code in a class does the this mean the class or someObject, one hopes it means the someObject.

    next is the scope issue i've talked about suppose i'm dynamically creating objects on the fly and want the callback to reflect the id thus

    for( i=0 ; i<10 ; i++ )
    {
      someObject[i] = new SomeObject();
      someObject[i].onclick = function(){ myFunction( i ) }
    }
    every single object will pass the value of 10 to myFunction, because after the function has finished the instance of i in memory that was used is still sat there and every myFunction has been given a pointer to it, not the value it was when it was initialised!

    so some oversights still exist, if only there were ways you could explicitly state "pointer to" or "value of" like in, oh, every other language including visual basic
    --
    If you don't risk failure you don't risk success.
  4. Re:Yes, but Javascript is a bad language. by masklinn · · Score: 4, Informative

    It is not possible to have two methods with the same name and different parameters (only one of those will ever be called).

    Because Javascript has no ways of dispatching: all functions (remember that Javascript methods are exactly like functions) use varags, and the arguments you ask for are but pointers to vararg cells.

    Example:

    1. function foo(a, b, c) {
    2. return [a, b, c, arguments]; // `arguments` holds every single argument of your function no matter what
    3. }
    4. foo() // -> [null, null, null, []]
    5. foo(1) // -> [1, null, null, [1]]
    6. foo("foo","bar","baz","buzz","bon") // -> ["foo","bar","baz",["foo","bar","baz","buzz","bon" ]]

    On the other hand methods behave like objects. One can have arrays of methods and then call mymethods[2](param);!

    It's not that JS functions "behave" like objects, JS function are objects, period. Callable objects maybe, but objects nonetheless, they're no different from strings, integers or lists in that aspect.

    And this is one of the nicest features of the language (along with lexical scoping and complete closures)

    --
    "The way we can tell it's C# instead of Haskell is because it's nine lines instead of two." -- wadler
  5. Re:I love the autopointerage & hate the scope by Shano · · Score: 4, Informative

    That isn't really an oversight, it's the way closures work. Most functional languages let you create closures explicitly so the problem doesn't arise. Javascript does it automatically, and usually when you don't expect it. In Javascript, you can do:

    someObject[i].onClick = function(i) { return function() { myFunction(i) } } (i);

    That creates a closure for each handler, with its own copy of i, so they will all get the values you want. I have no doubt there are other ways to do it, but this works for me.

  6. Re:JS by HxBro · · Score: 4, Informative

    After writing javascript for the past 6 years on digital tv platforms, I can say I've seen EPG's, Games, Apps running on liberate based set-top boxes and various IPTV set-top boxes could be running apps of similar size at time too.

    Granted you do try keep the sizes down but in some cases especially and EPG you do end up writing lots of code.

  7. Re:JS by h2g2bob · · Score: 4, Informative

    How about Mozilla firefox (or any of it's extensions).

  8. Re:JS by foniksonik · · Score: 4, Informative

    How about Dreamweaver? Have you ever looked at it's WYSIWYG code? All Javascript. In fact the entire UI is 90% Javascript.... you can customize the whole app by editing, you guessed it Javascript.

    --
    A fool throws a stone into a well and a thousand sages can not remove it.