Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
×
Programming IT Technology

Should JavaScript Get More Respect? 439

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."
This discussion has been archived. No new comments can be posted.

Should JavaScript Get More Respect?

Comments Filter:
  • by tpwch ( 748980 ) <slashdot@tpwch.com> on Wednesday December 20, 2006 @05:53AM (#17310468) Homepage
    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.
  • by Anonymous Coward on Wednesday December 20, 2006 @05:59AM (#17310498)
    Hey guys. I'm an online gamemaker, so 'toy' languages are right up my alley.

    The main problem with writing games with some of the most applicable web tools out there (Javascript, Flash) is that once it hits the web anyone with access to the View Source command can steal your work and throw it on their own site as theirs. This is highly discouraging.

    Nowadays I do use Javascript and Flash extensively, but the most significant part of any game machinery is always on the backend somewhere, usually in PHP.
  • by Myen ( 734499 ) on Wednesday December 20, 2006 @06:02AM (#17310518)
    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])
    }
  • by cyclomedia ( 882859 ) on Wednesday December 20, 2006 @06:06AM (#17310534) Homepage Journal
    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 [digitalmars.com], 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
  • by masklinn ( 823351 ) <.slashdot.org. .at. .masklinn.net.> on Wednesday December 20, 2006 @06:10AM (#17310552)

    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)

  • by Myen ( 734499 ) on Wednesday December 20, 2006 @06:21AM (#17310590)
    Sadly, when Javascript gets mentioned most people think of browser scripting. That's like thinking of MFC every time C++ gets mentioned... :(

    What sorts of shells interpret JS? I know of Mozilla's js shell [mozilla.org], and they also have a xpcshell (which adds XPCOM things to make it fully Mozilla-y). Sadly js shell has no built-in file access (it's a compile-time option you have to jump through hoops to enable, and buggy), and xpcshell has lots of XPCOM baggage.

    Are there any others using different engines? Anything from Adobe (ActionScript) maybe?
  • by Shano ( 179535 ) on Wednesday December 20, 2006 @06:37AM (#17310670)

    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.

  • by Anonymous Coward on Wednesday December 20, 2006 @06:37AM (#17310674)
    Parent wrote:

    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
    Well if you don't like that, then just make a function that its arguments by lexical closure:

    make_function = function (n) function() { myFunction( n ) };
    for( i=0 ; i<10 ; i++ )
    {
      someObject[i] = new SomeObject();
      someObject[i].onclick = make_function (i);
    }
  • Re:JS (Score:4, Informative)

    by HxBro ( 98275 ) on Wednesday December 20, 2006 @06:52AM (#17310736)
    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.
  • Re:JS (Score:5, Informative)

    by h2g2bob ( 948006 ) on Wednesday December 20, 2006 @06:55AM (#17310746) Homepage
    How about Mozilla firefox [mozilla.org] (or any of it's extensions).
  • by Gavin86 ( 856684 ) <gavin@b@lynch.gmail@com> on Wednesday December 20, 2006 @06:56AM (#17310752) Journal
    Javascript is anything but dense. The most impressive part about the various flexible agents is that they are easily understandable programming patterns. That makes it very easy to make an assessment for which methodology you will employ.
  • Re:Dense != Good (Score:3, Informative)

    by Eivind Eklund ( 5161 ) on Wednesday December 20, 2006 @07:09AM (#17310818) Journal
    I consider the numbers in that comparison so deeply flawed that they can't be used for comparing density of two languages like this.

    In my experience, Ruby is about twice as dense as Perl *in direct translation* (I have taken Perl libraries and translated directly to Ruby). It is even more dense when the code is idiomatic Ruby - that might be up to 10x. Idiomatic Common Lisp is about as dense as Ruby.

    Yet, Perl comes out at 15 and Common Lisp comes out at 5 in that "programming languages comparison", and Java comes out above Common Lisp at 6. These results are completely ridicilous. They probably have some statistical correlation with reality for some kinds of programming with some kind of developers - they're just far from exact enough to be useful for specific language comparison, like you do above. (They're also a decade out of date.)

    Eivind.

  • by Anonymous Coward on Wednesday December 20, 2006 @07:12AM (#17310832)
    Java 5 has vargargs.
  • Re:JS (Score:2, Informative)

    by datadriven ( 699893 ) on Wednesday December 20, 2006 @08:40AM (#17311174) Homepage
    for (i=25000;i=1;i--) {
        document.write((i + ' bottles of beer on the wall,' + i + ' bottles of beer. Take one down and pass it around ' + (i-1) + ' bottles of beer on the wall\n');
    }

    I only had 3 lines.
  • by Anonymous Coward on Wednesday December 20, 2006 @08:44AM (#17311194)
    Beside the lack of a good way to debug your javascript code

    Ahem.
    FireBug [joehewitt.com].
  • Re:JS (Score:2, Informative)

    by Schraegstrichpunkt ( 931443 ) on Wednesday December 20, 2006 @08:48AM (#17311226) Homepage

    'integrated JavaScript code'
    ...as opposed to what? JS that isn't integrated...?

    Perhaps I chose my phrasing poorly. The JavaScript interpreter is integrated into the browser, and has direct access to the web page's content. As opposed to Java applets, which are mostly isolated from the browser and the surrounding page content.

    For the grammar goons among us:
    applet ['aplit ] noun - Computing A very small application, esp. a utility program performing one or a few simple functions.

    I don't get my computing vocabulary from some unnamed dictionary. I get it from usage, and I've never heard anyone who isn't confused use "applet" in reference to JavaScript code. You wouldn't use it either, if you actually cared to communicate your thoughts clearly.

  • by ajs318 ( 655362 ) <sd_resp2@earthsh ... .co.uk minus bsd> on Wednesday December 20, 2006 @09:04AM (#17311340)
    It's not a "very minor" issue. It's fundamentally broken. Essentially, everytime you want to add numbers, you end up having to subtract a negative number instead, or use ParseInt() / ParseFloat() to force things to be numeric (concatenating strings seems to be the default behaviour of +). This just looks messy (and coming from someone who uses Perl, that's a damning indictment indeed).

    Your strcat() example is nice, but it can only ever concatenate two strings -- a limitation of the language. (JavaScript insists for every function to have an exact number of parameters; Perl functions can take as many or as few parameters as you like). So it soon gets unwieldy; you need separate functions to concatenate three or four strings. Or, more likely, to add different numbers of numbers:
    function sum2(n1, n2) { return (n1 - (-n2)); }
    function sum3(n1, n2, n3) { return (n1 - (-n2-n3)); }

    and so forth.

    I think awk has the best syntax for string concatenation .....
  • Re:Dear god no. (Score:2, Informative)

    by greyc ( 709363 ) on Wednesday December 20, 2006 @09:09AM (#17311362)
    Blargh. I should take a bit more time to think before posting. What I meant was:

    class foo:
          def __init__(self, n):
                  self.n = n
          def __call__(self, i):
                  self.n += i
                  return self.n
  • by Zardoz44 ( 687730 ) on Wednesday December 20, 2006 @09:43AM (#17311600) Homepage
    How [jsunit.net] so? [sourceforge.net]
  • Re:Dense != Good (Score:3, Informative)

    by MemoryDragon ( 544441 ) on Wednesday December 20, 2006 @10:52AM (#17312356)
    one word check out the dojo toolkit www.dojotoolkit.org, then you can see which codesizes we are already dealing with. The dojo toolkit is not the only javascript codebase of significant size already outside in the wild, the more interactive the applications become the more you see toolkits of that size becoming the norm not the exception.
  • Re:JS (Score:5, Informative)

    by foniksonik ( 573572 ) on Wednesday December 20, 2006 @11:31AM (#17312934) Homepage Journal
    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.

  • by arevos ( 659374 ) on Wednesday December 20, 2006 @11:55AM (#17313286) Homepage

    The thing that makes it messy in C is C's strong typing, you would have to pass and return everything as strings, but this is to be expected since the problem is contrived to demonstrate the brevity possible with weak-typing. The trade off is that you don't know exactly what your dealing with. In well written scripts that's no big deal, in a system with thousands of source files written by dozens of programmers it will quickly turn your brain to mush.

    It used to be that weak typing in a language was a BadThing(TM), now it's a "feature".

    You're confusing weak typing with dynamic typing. C is not strongly typed; it has static weak typing. Languages like Java have static strong typing, Python and Ruby have dynamic strong typing, and PHP and Javascript have dynamic weak typing.

  • by Scarblac ( 122480 ) <slashdot@gerlich.nl> on Wednesday December 20, 2006 @01:41PM (#17314720) Homepage
    Python and Ruby may be "dynamic" in some sense, but in both of them string += int will cause an exception. They're strictly typed, just with dynamic binding. Javascript really is a bit odd in allowing += for both addition and concatenation, and giving no warnings if you mix types. That's the sort of fuzzy thing that you'd only expect in PHP.
  • Re:JS (Score:3, Informative)

    by rjshields ( 719665 ) on Wednesday December 20, 2006 @02:41PM (#17315480)
    The only reason some people thought about Sun Java Applets is that Sun promoted that term a lot back in the day. As someone else said, this is supposed to be "news for marketdroids", it's news for nerds, and nerds are supposed to know the difference between a JS powered applet, and a Sun Java Applet.

    Of course everyone should know that Sun didn't invent the term "applet", no one is disputing that. However, when you start talking about applets in the context of web pages and web development, it's only logical to assume you're talking about Java applets, and certainly not bits of JavaScript. You don't use the term "applet" to describe bits of JavaScript unless you're very confused. To quote wikipedia [wikipedia.org] :-

    An applet is a software component that runs in the context of another program, for example a web browser...An applet is written in a language that is different from the scripting or HTML language which invokes it. The applet is written in a compiled language, while the scripting language of the container is an interpreted language, hence the greater performance or functionality of the applet.

    I'm sure you can see why confusion should arise over incorrect use of the term "applet".

  • by Anonymous Coward on Wednesday December 20, 2006 @03:06PM (#17315804)
    Scheme is a lisp dialect. Lispers and Schemers both agree on this. I'll take their word over your ranting.

  • by freezin fat guy ( 713417 ) on Wednesday December 20, 2006 @04:00PM (#17316418)
    The problem with developing Javascript code is that you are shooting at a moving target.

    Unless the use is restricted to a highly controlled intranet setting it will be executed on an indeterminate set of runtime environments. Different browser vendors, different versions, different sub-builds... where does the madness end?

    Unless you are doing something trivial you can wind up with several times the code necessary to get the job done on any one Javascript runtime. And bug testing? Well that takes far longer than it should for exactly the same reason.

    I don't have a problem with the language itself. Or any one interpretation of the language to be more precise. But give me some solid footing.

    Beef #2 - is your Javascript accessible to disabled users? Standard response: "F*** the disabled; they're a minority and we all know minorities deserve to be shot and pissed on." As I lack the Satanic vitriol necessary to punish people for unfortunate circumstances I find myself at odds with the Web 2.0 community.

Lots of folks confuse bad management with destiny. -- Frank Hubbard

Working...