Skip to content

Comment on JavaScript: what is "this"?

Comments

It's not entirely our fault, the language was designed to work like one thing (scheme-like), but look like another (c-like).

The baseless assertion rears its head again. People keep repeating this, and it's not true. JavaScript is not Scheme. It's not any more Scheme-like than Java, C++, Python, Perl, or many other languages. Lua and Ruby are far more Scheme-like than JavaScript.

I've commented before on people repeating this statement without ever examining it. I don't want to dredge up the arguments against it again ( http://news.ycombinator.com/item?id=1171202 ), mostly I just want to whine at it because it's so annoying. Why do you have to make JavaScript seem like something it's not? Are you trying to seem cooler by equating the language you work in to Scheme? What's wrong with JavaScript just being what it is?

I think the author is just citing how influential Scheme was to JavaScript's design.

In "Coders at Work", page 141, Brendan Eich talks about wanting to build a scheme-like language for the browser. The c-like syntax was adopted because Netscape wanted it to look like Java.

Maybe he wanted to. If that was his goal, I think he failed.

    Scheme: First-class functions, tail-call elimination, macros,
            hygienic macros, write and parse the AST directly.
    JavaScript: First-class functions.
    
    Scheme: Immutable arrays and lists, mutable strings.
    JavaScript: Mutable-only arrays, immutable-only strings.
Yes, I have, and have read, Coders at Work. I don't see how it's relevant. JavaScript and Scheme share only one defining feature in common, first-class functions. It's an important feature, no doubt, but many other languages have it: C/Objective-C (with Apple's blocks extension), C++ (with Boost.Lambda), Perl, Python, Ruby, Lua, Io, OCaml, ML, Haskell, the list goes on. People compared Perl to Lisp once, briefly, when it was one of the few dynamic languages, but they don't anymore.

They share only one defining feature? Bullshit. If we include "garbage collected", that gets rid of C and C++. If we include "dynamically typed", that gets rid of OCaml, ML, Haskell. If we include "single namespace for functions and variables", that gets rid of Ruby, Io, and Perl. Now we're down to JavaScript, Python, and Scheme, which, yes, are all in basically the same family of languages. If we include the (admittedly subjective) feature "small language spec", that gets rid of Python, and we're left with JavaScript and Scheme as the only two mainstream languages with those particular defining features.

If I were describing JavaScript to someone who was a programming language expert but somehow was never exposed to it, I would describe it as "Scheme, with C syntax, no tail calls, and a strange prototype-based object system".

People compared Perl to Lisp once, briefly, when it was one of the few dynamic languages, but they don't anymore.

They should; Perl 5 compares quite well and Perl 6 fares even better (of the nine fundamental features of a Lisp, lacking only homoiconicity). See the book Higher-Order Perl, for example.

http://hop.perl.plover.com/

Oh, come on, you're cherry picking the features that suit your argument. If we include "garbage collected", that gets rid of C and C++. If we include "dynamically typed", that gets rid of OCaml, ML, Haskell. If we include "single namespace for functions and variables", that gets rid of Ruby, Io, and Perl. Now we're down to JavaScript, Python, and Scheme, which, yes, are all in basically the same family of languages. If you add the (admittedly subjective) feature "small language spec", that gets rid of Python, and we're left with JavaScript and Scheme as the only two mainstream languages with that particular feature set.

You have to compare JavaScript with Scheme of the time when JavaScript was designed. Both macros and immutable structures are pretty recent (1999 and 2006 IIRC) additions to Scheme, previously such facilities were either non-existent or implementation-specific.

While Scheme's syntax superficially looks like it's data, specification does not imply that there should be accessible AST as list structure anywhere (in contrast to Common Lisp, which is specified in terms of list structures and algorithm to build them from text serialization)

And by the way: You have left off the one most important defining feature of Scheme: first-class continuations. And JavaScript really does not have them (I would say that this is completely reasonable omission for JS's purpose)

JavaScript definitely isn't Scheme (just as it definitely isn't Java either), but it is correct that it was conceived as a somewhat Scheme-like language with an OO-model inspired by Self, but for marketing reasons it was given a superficially Java-like syntax (and name!).

It is interesting to imagine what could have happened if Netscape had shipped with a Lisp. Probably a lot more people would hate Lisp today!

Egh, some of those other languages are more Scheme-like than others. In particular, Javascript is certainly more scheme-like than Java, which lacks even first-class functions, for example. And, well, Python is basically just Lisp with different syntax (or so Norvig claims; I'm not sure I'd go that far: http://norvig.com/python-lisp.html).

So I can't really agree that it's not any more Scheme-like than anything else out there. I do agree it's not particularly Scheme-like.

Javascript's scoping is also closer to Scheme's lexical scoping, I think. If you always use var for local variables, their scope will always be the immediately enclosing function. With Python, the scope of a variable is less clear. To me, the combination of first class functions, anonymous function syntax, and lexical scope are very high on the list of features that make a language "Scheme-like."

How are Python's scoping rules less clear? They are identical to Javascript's (and Scheme's), except that instead of defaulting to global scope, they default to local scope (which can be overridden with `global foo`). Also, it has one extra kind of scope, class scope. I would agree with you if you said Python's class scope is rather strange, of course.

This point of view is also maintained by D Crockford[1] who is supposed to know JavaScript's background.

[1] IIRC eg in this presentation: http://www.infoq.com/presentations/The-State-and-Future-of-J...

"Some other guy who other people say is right said something similar. Here is a link to a talk he once did."

Some guy who happens to be a member of the ecmascript committee ...

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.