Live data from Hacker News

John Resig: JavaScript as a First Language

ejohn.org

201–210 of 228 posts

Re: John Resig: JavaScript as a First Language

#201

All decent suggestions. I quibble with this, though: // Don't do this: function getData() { } // Do this instead: var getData = function() { }; The assignment is righteous, but by omitting the function’s name, you make stack traces more difficult to follow. Better this instead: var getData = function getData() { };

I have to disagree. Part of the reason is that it teaches the concept of anonymous functions.

The main reason, though, is the anonymous function case makes it more apparent that the function is an object.

Separating the RHS from the LHS is significant. The right side creates the object, the left side binds it to the reference. Much like `var a = 3`.

It is the assignment that makes it interesting for people that I teach JavaScript. Consider the following, can you do that in a less function-oriented language like Java easily?

     var getData = function () {};


     var mygetdata = getdata;

     getData("foo")

     mygetData("foo")

Re: John Resig: JavaScript as a First Language

#202
post #148

Earlier quoted context omitted.

> but later in the code it was important that the value was actually 5 and not "5". The bug was in that part and not in previous if(5=="5") part. Issues like these appear when Java programmers start to code PHP as if it's Java.

I agree - in fact the problem was solved by removing some unnecessary type-checking code. The point I was trying to make is that it's easier to learn the basics of types and logic in a strict system. Implicit type conversion and polymorphism can easily result in misunderstandings when you've just started learning to program.

"Easier" is (usually) the enemy of "better".

Re: John Resig: JavaScript as a First Language

#203

Earlier quoted context omitted.

Upvote from me. It's a sad day when Kiddiescript is somehow magically elevated to the status of a real programming language.

JavaScript is Lisp in C's clothing. http://javascript.crockford.com/javascript.html Also this: http://gigaom.com/cloud/node-js-and-the-javascript-age/

If this were actually true, JavaScript would have TCO by now.

Re: John Resig: JavaScript as a First Language

#204

Earlier quoted context omitted.

Seeing as how one of the arguments for teaching JavaScript first is that it lets you teach about prototypical inheritance before the students get used to class-based inheritance, the complexity of simulating a class in JavaScript is probably not the best objection to using it instead of CoffeeScript. If you teach them CoffeeScript, then you will teach them class-based inheritance just like everywhere else, and miss o…

I'm ok with missing out on the prototype paradigm. To me, it is a wart for JavaScript. There has been a ton of discussion on why libraries like DateJS, which pollute the Date prototype, are bad. http://stackoverflow.com/questions/4823682/javascript-namesp...

The discussion you're linking to doesn't appear to discuss either general problems with the prototype paradigm or specific problems with adding properties/methods to the Date prototype.

The closest it seems to come is a warning about not trying to modify the prototype of an object through its __proto__ property (or, I suppose, .getPrototypeOf), which isn't the same thing.

Re: John Resig: JavaScript as a First Language

#205
post #120

Earlier quoted context omitted.

> The reason Haskell is viewed as hard is that it takes basically nothing from languages you already know. If you don't know any languages, it's actually a great starting point. At my undergrad program, Haskell was used for the second CS course after the basic intro course. It was a nightmare and drove people away from the major in droves. They just did a curriculum overhaul and one of the changes the CS faculty are…

What are you doing in CS if Haskell "crushes your spirit"? Go study finance.

Funny you should say that--finance is probably the field that uses the most Haskell :)

Re: John Resig: JavaScript as a First Language

#206

Earlier quoted context omitted.

You're looking at the wrong side of it. The subtraction does behave in a perfectly logical manner. The problem is that, given the the behavior of the subtraction operator, the addition operator's actions are illogical. Specifically, I'd argue it's perverse because it breaks commutativity: '5' + 3 - 3 != '5' - 3 + 3 The logical approach would be to only assume that '+' is a string concatenation if both operands are st…

That would be ridiculous because then what would 'hello' + 1 equal? Everyone would expect it to equal 'hello1'.

Huh? Everyone? I expect it to be 'hellp' because I am thinking it works similar to Ruby's 'hello'.succ

Re: John Resig: JavaScript as a First Language

#207

Earlier quoted context omitted.

I agree - in fact the problem was solved by removing some unnecessary type-checking code. The point I was trying to make is that it's easier to learn the basics of types and logic in a strict system. Implicit type conversion and polymorphism can easily result in misunderstandings when you've just started learning to program.

"Easier" is (usually) the enemy of "better".

Again. These are classes for _beginners_. I'm not suggesting that you should never learn these things. They should also learn functional programming, estimating how algorithms perform, concurrent programming, binary trees, and a few hundred other things. But Programming is not something you master overnight.

You need to understand that 2+2=4 before you can understand that 2+i^2=1.

Re: John Resig: JavaScript as a First Language

#208
post #89

Earlier quoted context omitted.

Well, I grew up in a day when it was even easier. * Buy home computer (ZX81) * Turn on * 10 Print "Hello world" * run I don't think the instant gratification and ease of use did me any harm.

Of course, the "buy home computer" step was a lot harder back in those days.

It wasn't. At least in the UK in the 80s, you could go out to any town center, walk into a shop, and buy a "home" computer. They were everywhere.

Re: John Resig: JavaScript as a First Language

#209

Earlier quoted context omitted.

That would be ridiculous because then what would 'hello' + 1 equal? Everyone would expect it to equal 'hello1'.

I would expect 'hello' + 1 to throw an exception. I would similarly expect '5' - 3 to throw an exception. Why? Because you can't add a string to an integer, and nor can you subtract an integer from a string. Doing anything else is arbitrary and unpredictable, IMO, leading to subtle type errors. You want to find type errors early as possible, rather than letting bogus values flow through the program. I think Javascrip…

You're conveniently ignoring the fact that the + operator also means string concatenation.

Javascript thinks to itself 'hello' string concatenated with a 1 value.

Re: John Resig: JavaScript as a First Language

#210

Earlier quoted context omitted.

Why is prompt() ugly? On the contrary, I think that for kids who grew up in GUIs, it is much more familiar and less intimidating than a prompt.

With a standard prompt, you can see the previous output from the program and user input. But a popup window has no context, no history. Of course that's just my opinion. If it could help teach kids good programming skills then it should be used used.

That's true - if your program works like a conversation. That's not always the case ;) You can give all the needed context by updating the page when needed.
Post reply on HN